2012-05-23 4 views
1

나는 특별한 시간 동안 푸시 알림을 사용하려고합니다. 이 코드를 사용하고 있습니다.알람 관리자를 사용하여 특정 시간 동안 Android 푸시 알림

이 내 AlarmReceiver 클래스입니다 :

public class AlarmReceiver extends BroadcastReceiver { 

private static int NOTIFICATION_ID = 1; 

@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager manger = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notification = new Notification(R.drawable.arrrow_icon, 
      "test", System.currentTimeMillis()); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 
      NOTIFICATION_ID, new Intent(context, AlarmReceiver.class),0); 

    Bundle extras = intent.getExtras(); 
    String title = extras.getString("title"); 
    String note = extras.getString("note"); 
    notification.setLatestEventInfo(context, note, title, contentIntent); 
    notification.flags = Notification.FLAG_INSISTENT; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    manger.notify(NOTIFICATION_ID++, notification); 

} 
}; 

그리고이 부분의 내 주요 클래스;

의도 alarmintent = 새로운 텐트 (getApplicationContext() AlarmReceiver.class); alarmintent.putExtra ("title", "test"); alarmintent.putExtra ("note", "test message"); PendingIntent 보낸 사람 = PendingIntent .getBroadcast (getApplicationContext(), 1, alarmintent, ) PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 나는 가까운 알림 작동하지 않는 강제 때

 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

내 응용 프로그램이 다시 작동하면

알림이 correctly.but 노력하고 밀어 넣습니다. 도와 주셔서 감사합니다.

답변

0

이 정보는 도움이 될 수 있습니다. 첫 번째 알람 수신기 클래스에이 코드를 사용하는 것은

public class AlarmReceiver extends BroadcastReceiver 
{ 
    NotificationManager nm; 
    public void onReceive(Context context, Intent intent) 
    { 
     Toast.makeText(context, "Log Out !!", Toast.LENGTH_LONG).show(); 
//  Vibrator vibrator=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); 
//  vibrator.vibrate(1000); 
     nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     CharSequence ch="Log Out Service"; 
     CharSequence message="Log Out You have to login again!! "; 
     Notification notif = new Notification(R.drawable.ic_launcher, 
        "You Are Logged out...", System.currentTimeMillis()); 
//  PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
//     new Intent(), 0); 
     Intent notifyintent=new Intent(context,AlarmManagerTestActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      notifyintent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
     notif.flags |=Notification.FLAG_AUTO_CANCEL; 
        notif.setLatestEventInfo(context, ch, message, pendingIntent); 
        nm.notify(0, notif);   
     } 
    } 

Intent intent = new Intent(your activity,AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(
AlarmManagerTestActivity.this, 0, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); 
+0

답장을 보내 주셔서 감사합니다.) 이것은 같은 결과입니다. 그것은 작동하지만, 응용 프로그램 메뉴에서 응용 프로그램을 강제 종료하면 알림이 작동하지 않습니다. – skhizein

+0

ok이 코드를 사용하려고 시도했습니다. –

0

확인 후,이 코드를 사용에서 onCreate() 보호 무효 onUserLeaveHint에서이 코드를 넣어보십시오 (당신의 활동에이 코드를 넣어) { // TODO 자동 생성 된 메서드 스텁 super.onUserLeaveHint(); exitAppAnimate(); } 당신이 보류 intent.this를 사용하는 것이 도움이 될 것입니다

private void exitAppAnimate() { 
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    List<RunningTaskInfo> recentTasks = activityManager 
      .getRunningTasks(Integer.MAX_VALUE); 
    for (int i = 0; i < recentTasks.size(); i++) { 
     if (i == 1 
       && recentTasks.get(i).baseActivity.toShortString().indexOf(
         getPackageName()) > -1) { 
      // home button pressed 
      IShomepressed = true; 
      System.out.println("Home buttonpressed.........."); 

      // android.os.Process.killProcess(android.os.Process.myPid()); 
      break; 
     } 
    } 
} and put this if (IShomepressed) { 
     IShomepressed = false; 

.

+0

죄송합니다. ı은 (는) onUserLeaveHint() 함수를 호출해야합니다.. 다시 감사합니다. – skhizein

+0

onUserLeaveHint를 사용해야합니다. onCreate() 메서드 밖에서() 메서드를 호출합니다. –

+0

다른 작업과 다르지 않습니다. – skhizein

관련 문제