0

来电报警:

        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是静态全局变量。我如何防止会话变为空

注意:我的警报类是一个内部静态类。设置闹钟后 10 秒后闹钟响起

4

1 回答 1

2

从战术上讲, delete android:process=":remote",事情可能会更好。

AlarmManager但是,更一般地说,如果您的应用程序在后台,您的进程可能会在调用之间终止。这是完全正常的,通常是用户想要的。您的代码需要处理这种情况。静态数据成员是一个缓存,仅此而已——任何需要在进程终止后幸存下来的东西都需要持久存储(例如,数据库、文件)。

于 2012-12-06T16:27:58.593 回答