簡體   English   中英

重復通知

[英]Repeating notification

我正在嘗試設置一個每天固定時間重復的通知。

我可以通過單擊列表中的項目來設置通知(但這並不是很有趣。。。)我已經嘗試了幾種方法,但是仍然無法使其自動顯示或每天顯示。

到目前為止,這是我所做的:

public class main extends Activity implements PersonneAdapterListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayList<com.example.joris.list3.Personne> listP = com.example.joris.list3.Personne.getAListOfPersonne();
    com.example.joris.list3.PersonneAdapter adapter = new com.example.joris.list3.PersonneAdapter(this, listP);

    adapter.addListener(this);

    ListView list = (ListView)findViewById(R.id.ListView01);

    list.setAdapter(adapter);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(main.this, NotifyService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(main.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) main.this.getSystemService(main.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

@Override
public void onClickNom(com.example.joris.list3.Personne item, int position) {
    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Personne");
    builder.setMessage("Vous avez cliqué sur : " + item.prenom);
    builder.setPositiveButton("OK", null);
    builder.show();

    //Building the Notification
    int NOTIFICATION_ID =1;
    Context context = getApplicationContext();
    NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context);
    builder1.setSmallIcon(R.drawable.eeo);
    builder1.setContentTitle("Green Reminder");
    builder1.setContentText(item.prenom);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder1.build());
}}

這是我的另一堂課:

public class NotifyService extends BroadcastReceiver {

public void onReceive (Context context, Intent intent){

            long when = System.currentTimeMillis();

            Intent notificationIntent = new Intent(context, main.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            int NOTIFICATION_ID=1;
            NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.eeo)
                    .setContentTitle("Green Reminder")
                    .setContentText("test")
                    .setSound(alarmSound)
                    .setAutoCancel(true)
                    .setWhen(when)
                    .setContentIntent(pendingIntent)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
        }
    }

謝謝

您是否已在清單中設置接收器? 我認為問題是您沒有收到Alarmmanager的廣播。 嘗試添加

intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
intent.addCategory("android.intent.category.DEFAULT");

在onCreate方法中使用intent1。

確保清單中也包含以下內容:

    <receiver android:name=".NotifyService" >
        <intent-filter>
            <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM