【问题标题】:Repeating notification重复通知
【发布时间】:2015-10-22 06:29:42
【问题描述】:

我正在尝试设置每天在固定时间重复的通知。

我可以通过单击列表中的项目来设置通知(但这并不是很有趣...) 我已经尝试了几件事,但我仍然不能让它自动出现,也不能每天出现......

这是我到目前为止所做的:

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());
        }
    }

谢谢

【问题讨论】:

    标签: android notifications alarmmanager repeat repeatingalarm


    【解决方案1】:

    您是否在清单中设置了接收器? 我认为问题是您没有收到来自警报管理器的广播。 尝试添加

    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>
    

    【讨论】:

    • 是的,我有。 ''
    • 也添加这个...
    • 是的,它有效!太感谢了 !你也许拯救了我的学期:)
    • 很高兴为您提供帮助。赞成或接受的答案将不胜感激:)
    • 我已经尝试过投票,但是由于我是 SO 新手,所以我不能:/ 我肯定会接受你的回答 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多