0

我制作应用程序的流程是_

1- 成功登录 facebook 后获取所有好友 ID 并存储到 Arraylist。2-之后我设置alarmManaget每天执行一次,代码如下:-

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendlist);
    lvfrnd = (ListView)findViewById(R.id.listview);
    Bundle mbundle = getIntent().getExtras();
    if(mbundle!=null && Constant.isService==false )
    {
        appId = mbundle.getString("appid");
    }
    if(mbundle!=null && Constant.isService==true )
    {
        isSuccess = mbundle.getString("success");
    }
    mUserIdFetcher = new UserIdFetcher();
    mfriendIdlist = mUserIdFetcher.getFriendIdList(appId);
    String friendId = makeStringOfId(mfriendIdlist);
    SharedPreferences mprePreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = mprePreferences.edit();
    editor.putString("userId", friendId);
    editor.commit();
    setAlarmManager(appId);
    setListView();
 }

private String makeStringOfId(ArrayList<String>  mfIdlist)
{
    int length =0;
    String midstring = "";
    if(mfIdlist!=null)
    {
         length = mfIdlist.size();
    }

    for(int mindex =0;mindex<length;mindex++)
    {
        midstring = midstring+mfIdlist.get(mindex)+"/";
    }
    return midstring;

}

private void setAlarmManager(String appid)
 {
    // add minutes to the calendar object
       Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY,10);
        updateTime.set(Calendar.MINUTE,42);

   Intent mintent = new Intent(this,BirthDayReciever.class);
   mintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mintent, PendingIntent.FLAG_CANCEL_CURRENT);
   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
   // should be AlarmManager.INTERVAL_DAY (but changed to 15min for testing)
   alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
   System.out.println("call--------------1");
 }

 private void setListView()
 {
     if(Constant.mfriendInfolist!=null &&Constant.mfriendInfolist.size()>0)
     {
       mlistcustomer = new ListCustomAdapter(Friendlist.this,Constant.mfriendInfolist);
       lvfrnd.setAdapter(mlistcustomer);
       mlistcustomer.notifyDataSetChanged(); 
     }
  }

在上面的活动中,当执行到达挂起的意图时发生此异常:-

     07-16 10:44:26.193: E/AndroidRuntime(773): Uncaught handler: thread main exiting due to uncaught exception
     07-16 10:44:26.333: E/AndroidRuntime(773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bday.Alarm/com.bday.Alarm.Friendlist}: java.lang.IllegalArgumentException: Can't use FLAG_RECEIVER_BOOT_UPGRADE here
   07-16 10:44:26.333: E/AndroidRuntime(773):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.app.ActivityThread.access$2200(ActivityThread.java:126)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.os.Handler.dispatchMessage(Handler.java:99)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at android.os.Looper.loop(Looper.java:123)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.app.ActivityThread.main(ActivityThread.java:4595)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at java.lang.reflect.Method.invokeNative(Native Method)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at java.lang.reflect.Method.invoke(Method.java:521)
  07-16 10:44:26.333: E/AndroidRuntime(773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at dalvik.system.NativeStart.main(Native Method)
    07-16 10:44:26.333: E/AndroidRuntime(773): Caused by: java.lang.IllegalArgumentException: Can't use FLAG_RECEIVER_BOOT_UPGRADE here
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.os.Parcel.readException(Parcel.java:1222)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.os.Parcel.readException(Parcel.java:1206)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:2065)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at android.app.PendingIntent.getBroadcast(PendingIntent.java:230)
    07-16 10:44:26.333: E/AndroidRuntime(773):  at com.bday.Alarm.Friendlist.setAlarmManager(Friendlist.java:102)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at com.bday.Alarm.Friendlist.onCreate(Friendlist.java:71)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   07-16 10:44:26.333: E/AndroidRuntime(773):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
   07-16 10:44:26.333: E/AndroidRuntime(773):   ... 11 more

广播类启动服务,它获取用户的 facebook 朋友信息名称、生日并设置到对象类中,然后将该类放入 arralist 并将该数组列表显示到 Listview 我正在使用广播来更新这个朋友列表,每天一次我的服务类是:-

 public static final String PREFS_NAME = "MyPrefsFile";
 String [] friendFBId=null;



public UIUpdater(String name)
{

    super("UIUpdater");
    System.out.println("call--------------3");
    SharedPreferences mprePreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    String stringofUserId = mprePreferences.getString("userId","");
    if(stringofUserId!=null)
    {
        friendFBId = stringofUserId.split("/"); 
    }


}

@Override
protected void onHandleIntent(Intent intent) 
{

    Log.d("check--------------", "entered DisplayLoggingInfo");

    URL img_value = null;

    Bundle params =new Bundle();
    int length = friendFBId.length;

    String mAccessdata=Facebook.getAccessToken();

         try {
             System.out.println("fdartr");
            params.putString("format", "json");
            params.putString("access_token", mAccessdata);
            if(friendFBId!=null && friendFBId.length>0)
            {
                FriendsObject mFriendsObject = null;

                for(int mindex =0;mindex<10;mindex++)
                {
                      mFriendsObject = new FriendsObject();
                     String url = "https://graph.facebook.com/"+friendFBId[mindex];
                     System.out.println("infourl   "+url);
                     String response = Util.openUrl(url, "GET", params);
                        JSONObject json = new JSONObject(response);

                        try {
                            String name=json.getString("name");
                            mFriendsObject.setName(name);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String fname = json.getString("first_name");
                            mFriendsObject.setFirst_name(fname);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String lname = json.getString("last_name");
                            mFriendsObject.setLast_name(lname);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String gender = json.getString("gender");
                            mFriendsObject.setGender(gender);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String location = json.getString("locale");
                            mFriendsObject.setLocale(location);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String web = json.getString("link");
                            mFriendsObject.setLink(web);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String bdate=json.getString("birthday");
                            mFriendsObject.setBirthday(bdate);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String username=json.getString("username");
                            mFriendsObject.setUsername(username);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            String imgurl =" http://graph.facebook.com/"+friendFBId[mindex]+"/picture";
                            System.out.println("imgurl  ========= "+imgurl);
                            mFriendsObject.setImgUrl(imgurl);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        System.out.println("================="+Constant.mfriendInfolist.size());
                        Constant.mfriendInfolist.add(mindex,mFriendsObject);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        Bundle mbundle = new Bundle();
        intent = new Intent(this,Friendlist.class);
        Constant.isService = true;
        mbundle.putString("success","SUCCESS");
        startActivity(intent);
}

执行广播后发生此异常:-

  07-16 11:39:01.443: E/AndroidRuntime(1012): Uncaught handler: thread main exiting due to uncaught exception
  07-16 11:39:01.463: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to instantiate service com.bday.Alarm.UIUpdater: java.lang.InstantiationException: com.bday.Alarm.UIUpdater
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at android.app.ActivityThread.handleCreateService(ActivityThread.java:2878)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at android.app.ActivityThread.access$3200(ActivityThread.java:126)
   07-16 11:39:01.463: E/AndroidRuntime(1012):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1986)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at android.os.Handler.dispatchMessage(Handler.java:99)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at android.os.Looper.loop(Looper.java:123)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at android.app.ActivityThread.main(ActivityThread.java:4595)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at java.lang.reflect.Method.invokeNative(Native Method)
  07-16 11:39:01.463: E/AndroidRuntime(1012):   at java.lang.reflect.Method.invoke(Method.java:521)
   07-16 11:39:01.463: E/AndroidRuntime(1012):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-16 11:39:01.463: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
   07-16 11:39:01.463: E/AndroidRuntime(1012):  at dalvik.system.NativeStart.main(Native Method)
  07-16 11:39:01.463: E/AndroidRuntime(1012): Caused by: java.lang.InstantiationException: com.bday.Alarm.UIUpdater
      07-16 11:39:01.463: E/AndroidRuntime(1012):   at java.lang.Class.newInstanceImpl(Native Method)
      07-16 11:39:01.463: E/AndroidRuntime(1012):   at java.lang.Class.newInstance(Class.java:1479)
     07-16 11:39:01.463: E/AndroidRuntime(1012):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2875)
      07-16 11:39:01.463: E/AndroidRuntime(1012):   ... 10 more
4

3 回答 3

1

Intent 标志应该放在 Intent 对象上(定义它们的地方)。创建待处理意图时的标志是不同的。

也不FLAG_RECEIVER_BOOT_UPGRADE是供应用程序使用的。它不在 SDK 中。

根本原因:不要将任何 FLAG_ACTIVITY_ 常量与 getBroadCast() 或 sendBroadcast() 一起使用。而是使用 intent.setFlags(0);

于 2012-07-16T06:12:12.680 回答
1

Some say this is a bug in Android and is removed in 4.4:

https://stackoverflow.com/a/22883183/305135

as a temporary solution you can use this :

notificationIntent.setFlags(0);
于 2014-10-31T08:02:46.367 回答
0

找出此意图标志出现的位置:FLAG_RECEIVER_BOOT_UPGRADE

然后删除它或找到一种不同的方式来做你想做的事情。

于 2012-07-16T05:57:42.030 回答