2014-03-06 3 views
7

잘 작동하는 안드로이드 알림 코드를 다음과 같이 시도했지만 안드로이드 앱을 시작할 때 알림 팝업이 나타납니다.48 시간마다 안드로이드 알림을 표시하는 방법은 무엇입니까?

나는 48 시간마다 알림을 표시하고 싶습니다. 어떻게해야합니까?

이 기능을 사용하려면 어떤 변경을해야합니까? 이 방법에서 일을

알림 코드

Intent notificationIntent = new Intent(MainActivity.this, Activity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0); 

     NotificationManager notificationManager = (NotificationManager) MainActivity.this 
        .getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification noti = new NotificationCompat.Builder(MainActivity.this) 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setTicker("ticker message") 

          .setWhen(System.currentTimeMillis()) 
          .setContentTitle("HELLO") 
          .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS") 
          .setContentIntent(contentIntent) 
          //At most three action buttons can be added 
          .setAutoCancel(true).build(); 
     int notifyID =0; 
     notificationManager.notify(notifyID, noti); 
+1

안녕이 이동 : [http://stackoverflow.com/questions/15449511/show-notification-in-every-3-sec-in-android](http : //stackoverflow.com/questions/15449511/show-notificatio n-in-every-3-sec-in-android) –

+1

또는 이것으로 이동 : [http://stackoverflow.com/questions/7077420/show-android-notification-every-five-minutes?rq=1] (http://stackoverflow.com/questions/7077420/show-android-notification-every-five-minutes?rq=1) –

+0

@SimplePlan 두 코드가 작동하지 않습니다. ( – Neo

답변

9

여기에 가까운 무언가가 당신이 당신의 주요 활동에 필요로이다 : 여기를해야 할 코드 조각의 ShowNotification.java라는 이름의 다른 파일에

Intent notificationIntent = new Intent(context, ShowNotification.class); 
    PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, 
                  PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    am.cancel(contentIntent); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
      + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent); 

그런 다음, 다음 (가정을 추가하여 주요 활동은) MainActivity라는 :

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

public class ShowNotification extends Service { 

    private final static String TAG = "ShowNotification"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Intent mainIntent = new Intent(this, MainActivity.class); 

     NotificationManager notificationManager 
      = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification noti = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 
           PendingIntent.FLAG_UPDATE_CURRENT)) 
      .setContentTitle("HELLO " + System.currentTimeMillis()) 
      .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS") 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setTicker("ticker message") 
      .setWhen(System.currentTimeMillis()) 
      .build(); 

     notificationManager.notify(0, noti); 

     Log.i(TAG, "Notification created"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 
+0

1 시간마다 설정하는 방법은 무엇입니까? – chris

+0

및 앱이 열려 있는지 여부를 알려주고 싶습니다. – chris

+0

setRepeating() 메소드의 두 번째 및 세 번째 인수는 각각 초기 알람의 시간과 후속 알람의 간격을 지정합니다. 사용할 단위는 AlarmManager.INTERVAL_HOUR입니다. 그러나 https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)의 설명서에 설명 된 것처럼이 방법은 더 이상 잘 작동하지 않습니다. API19 이상을 사용하는 경우 기간이 짧습니다. – scottt

1

: - 알림이 활동의 ​​시작에서 열립니다. - 타이머를 추가하면 앱을 연 경우에만 타이머가 표시됩니다.

귀하의 경우, 서비스를 시작해야하며, 48 시간마다 Androis O.S에게 알림을 보내는 타이머가 있어야합니다. 이 루프를 수행하려면

것은, 당신이 티 메르가 필요합니다, 당신은 여기에 서비스에 대한 자세한 내용을 볼 수 있습니다 당신은 아마 당신의 알림을 예약 할 알람 관리기 클래스를 사용하려면 http://www.vogella.com/tutorials/AndroidServices/article.html

1

.

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
alarmManager.cancel(contentIntent); 
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
           System.currentTimeMillis() + AlarmManager.INTERVAL_DAY * 2, 
           AlarmManager.INTERVAL_DAY * 2, 
           contentIntent); 
+0

감사합니다. 'Intent notificationIntent'와 'Notification'입니다. 코드? – Neo

+0

그리고 내 전화를해야만 내 앱이 시작될 때마다 그것을 가져 오지 않아야합니다. – Neo

+1

보류중인 의도에서 알림 코드를 만들고 실행하는 별도의 공용 클래스를 지정해야합니다. 이미 가지고있는 동일한 기본 알림 코드를 사용할 수 있습니다. – scottt

관련 문제