呼叫闹钟:
Log.e("call: ", "calling alarm");
sessionForPost = Session.getActiveSession();
if(sessionForPost != null)
Log.e("session: ", "not null");
else
Log.e("session: ", "null");
Alarm alarm = new Alarm();
alarm.SetAlarm(getActivity().getApplicationContext());
警报管理员课程:
public static class Alarm extends BroadcastReceiver
{
static String victimId = null;
static Context context;
public Alarm(Context context){
Alarm.context = context;
}
public Alarm(){
if(sessionForPost != null){
Log.e("Alarm session: ", "not null");
}
else
Log.e("Alarm session: ", "null");
}
@SuppressLint("Wakelock")
@Override
public void onReceive(final Context context, final Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wak up lo");
wl.acquire();
if(sessionForPost != null)
Log.e("onReceive session: ", "not null");
else
Log.e("onReceive session: ", "null");
postFromAlarm(sessionForPost);
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
if(sessionForPost != null)
Log.e("SetAlarm session: ", "not null");
else
Log.e("SetAlarm session: ", "null");
Alarm.context = context;
Log.e("setalarm: ", "i am here");
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(contextForPost.getApplicationContext(), Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(Alarm.context, 2, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 1000 * 10, pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
postFromAlarm()方法:
public static void postFromAlarm(Session session){
String victimId;
if(flag){
Log.e("flag: ", "true");
}else{
Log.e("flag: ", "false");
}
if(Session.getActiveSession() != null)
Log.e("session: ", "not null");
else
Log.e("session: ", "null");
if(Session.getActiveSession() != null){
Log.e("onRecieve: ", "i am here");
Bundle postParams = new Bundle();
postParams.putString("message", msg);
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(contextForPost,
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(contextForPost,
postId,
Toast.LENGTH_LONG).show();
}
//Log.e("Ashchi to: ",error.getErrorMessage());
}
};
Log.e("Ashchi to2: ","poststory class");
if(friendId != null){
victimId = friendId;
}else{
victimId = user_ID;
}
Request request = new Request(Session.getActiveSession(), victimId+"/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
flag = true;
}
}
当我调用SetAlarm()时,现在是LogCat:
12-06 14:55:53.757: call: calling alarm
12-06 14:55:53.767: session: not null
12-06 14:55:53.787: Alarm session: not null
12-06 14:55:53.787: SetAlarm session: not null
12-06 14:55:53.787: setalarm: i am here
然后当报警触发LogCat给我时:
12-06 14:56:04.437: Alarm session: null
12-06 14:56:04.457: onReceive session: null
12-06 14:56:04.457: flag: false
12-06 14:56:04.487: session: null
警报管理员的清单:
......
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
......
<application
.....
<receiver android:process=":remote" android:name="com.timelystatusupdater.MyLoggedInFragment$Alarm"></receiver>
.....
正如您所看到的,当我呼叫闹钟并查看Session
时,它不为空。但是当警报触发时,会话变为空。问题是什么。记住sessionForPost
是静态全局变量。 如何防止会话变为空
N.B:我的闹钟类是一个内部静态类。 警报设定后10秒后发出警报
答案 0 :(得分:2)
从战术上讲,删除android:process=":remote"
,事情可能会更好。
但是,更一般地说,如果您的应用在后台,您的流程可能会在AlarmManager
的调用之间终止。这是完全正常的,通常是用户想要的。您的代码需要处理这种情况。静态数据成员是一个缓存,仅此而已 - 需要在进程终止后继续存在的任何东西都需要持久存储(例如,数据库,文件)。