호출 경보 :알람 매니저 때 경보 화재
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()을 호출하면 :
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
다음 알람 화재 로그 캣이 날 줄 때 : 알람 관리자에 대한
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로 바뀝니다. 무엇이 문제인가. sessionForPost
이 정적 전역 변수임을 기억하십시오. 어떻게하면 세션이 null로 바뀌는 것을 방지 할 수 있습니까?
N.B : 내 알람 클래스는 내부 정적 클래스입니다. 알람이 설정된 후 10 초 후에 알람이 울리고 있습니다.
u pls가 문제 해결 방법을 알려줍니다. 나를 위해 작동하지 않는 해결책이 아직 null 포인터 예외를 반환합니다. – user3233280