Search code examples
androidalarmmanagerandroid-alarms

alarm manager turning my variables into null when the alarm fire


calling alarm:

        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());

alarm manager class:

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() method:

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;
}

}

now the LogCat when i call SetAlarm() :

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

and then when the alarm fire LogCat give me:

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

my manifest for alarm manager:

......
 <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
......
<application
.....
<receiver  android:process=":remote" android:name="com.timelystatusupdater.MyLoggedInFragment$Alarm"></receiver>
.....

as you can see, when i call the alarm and check out the Session, it is not null. But when the alarm fire the session is turning into null. what is the problem. remember sessionForPost is static global variable. how can i prevent session to be turning null

N.B: my alarm class is an inner static class. the alarm is firing after 10 second when the alarm is set


Solution

  • Tactically, delete android:process=":remote", and things may work better.

    However, more generally, your process may be terminated in between invocations of AlarmManager, if your app is in the background. This is perfectly normal and usually is what the user wants. Your code needs to handle this situation. Static data members are a cache, nothing more -- anything that needs to survive a process termination needs to be stored persistently (e.g., database, file).