我制作应用程序的流程是_
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