2012-12-06 3 views
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()을 호출하면 :

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 초 후에 알람이 울리고 있습니다.

+0

u pls가 문제 해결 방법을 알려줍니다. 나를 위해 작동하지 않는 해결책이 아직 null 포인터 예외를 반환합니다. – user3233280

답변

2

전술적으로 android:process=":remote"을 삭제하면 문제가 해결 될 수 있습니다.

그러나 일반적으로 앱이 백그라운드에있는 경우 AlarmManager의 호출 사이에서 프로세스가 종료 될 수 있습니다. 이것은 정상적으로 정상적으로 수행되며 일반적으로 사용자가 원하는 것입니다. 코드가이 상황을 처리해야합니다. 정적 데이터 멤버는 캐시입니다. 프로세스 종료를 견뎌내려면 데이터베이스, 파일과 같이 영구적으로 저장해야합니다.

+0

감사합니다. 지금 일하고있다. 난 그냥'android : process = ": remote"' @CommonsWare – Shoshi

+0

@CommonsWare 나는 단지 이렇게하지만, 내 경보 관리자가 경고를 시작하지 않으면 pls 내가 무슨 문제를 말해 줄래? – user3233280