簡體   English   中英

無法使AlarmManager和多個通知正常工作

[英]Cant get AlarmManager and Multiple Notifications to work

好吧..我試圖找出三個問題。

  1. 我無法使AlarmManager通知我
  2. 我想為每個通知者顯示不同的消息

如果有人可以幫助我,我將感到很高興,我不會在這里發布此代碼,只是為了獲得代碼。我已經工作了2天,所以我想在這里咨詢專家。

如果我能找到可以檢查以下代碼的人,請顯示我的錯誤並指出糾正錯誤的方向。。謝謝yhu。

這是我的Notifier類,在該類中我設置了想要alertManager發送警報的日期和時間。

public class Notifier extends Activity {
private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_beginning);

    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    Calendar calendar3 = Calendar.getInstance();
    Calendar calendar4 = Calendar.getInstance();

    calendar1.set(Calendar.DAY_OF_WEEK, 1); // Sunday
    calendar2.set(Calendar.DAY_OF_WEEK, 4); // Wednesday
    calendar3.set(Calendar.DAY_OF_WEEK, 6); // Friday
    calendar4.set(Calendar.DAY_OF_WEEK, 5); // Thursday
    calendar4.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of Each Month

    // Sunday
    calendar1.set(Calendar.HOUR_OF_DAY, 5);
    calendar1.set(Calendar.MINUTE, 0);
    calendar1.set(Calendar.SECOND, 0);
    calendar1.set(Calendar.AM_PM, Calendar.PM);

    // Wednesday
    calendar2.set(Calendar.HOUR_OF_DAY, 17);
    calendar2.set(Calendar.MINUTE, 30);
    calendar2.set(Calendar.SECOND, 0);
    calendar2.set(Calendar.AM_PM, Calendar.PM);
    // Friday
    calendar3.set(Calendar.HOUR_OF_DAY, 17);
    calendar3.set(Calendar.MINUTE, 30);
    calendar3.set(Calendar.SECOND, 0);
    calendar3.set(Calendar.AM_PM, Calendar.PM);
    // Thursday
    calendar4.set(Calendar.HOUR_OF_DAY, 9);
    calendar4.set(Calendar.MINUTE, 0);
    calendar4.set(Calendar.SECOND, 0);
    calendar4.set(Calendar.AM_PM, Calendar.AM);

    Intent myIntent = new Intent(Notifier.this, MyBReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(Notifier.this, 0, myIntent,
            0);

    AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
            pendingIntent); // every Sunday
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
            pendingIntent); // every Wednesday
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
            pendingIntent); // every Friday
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28,
            pendingIntent); // every first Thursday

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 30,
            pendingIntent);

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31,
            pendingIntent);
} // end onCreate
}

這是我的接收器類

public class MyBReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent myservice = new Intent(context, MyAlarmService.class);
    context.startService(myservice);

}
}

這是我的警報服務,用於設置通知程序(這里只有一個通知程序),我不知道如何為接下來的三個警報設置另一個通知程序。

public class MyAlarmService extends Service {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
private NotificationManager mManager;
private Context context;

// Notification notification;

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

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    onStartCommand(intent, 0, startId);
}

@SuppressWarnings({ "static-access", "deprecation" })
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mManager = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            Notifier.class);
    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        Notification notification = new Notification(
                R.drawable.ic_launcher, "This is a test message!",
                System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent
                .getActivity(this.getApplicationContext(), 0, intent1,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(this.getApplicationContext(),
                "AlarmManagerDemo", "This is a test message!",
                pendingNotificationIntent);

        mManager.notify(0, notification);
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("AlarmManagerDemo1")
                .setContentText("This is a test message");

        Intent notificationIntent = new Intent(this, Notifier.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
    }
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

謝謝。

為每個警報使用不同的myIntent。 另外,為避免android優化它,請向意圖添加其他數據

myIntent = new Intent(Notifier.this, MyBReceiver.class);
data = Uri.withAppendedPath(Uri.parse("anything" + "://something/different");
myIntent.setData(data);

暫無
暫無

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

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