呼び出しアラーム:
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
、null ではありません。しかし、アラームが発生すると、セッションは null になります。何が問題ですか。remembersessionForPost
は静的グローバル変数です。セッションが null にならないようにするにはどうすればよいですか
注意: 私のアラーム クラスは内部静的クラスです。アラームを設定すると、10 秒後にアラームが作動します