不能让AlarmManager和多通知工作 [英] Cant get AlarmManager and Multiple Notifications to work

查看:78
本文介绍了不能让AlarmManager和多通知工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定..我想弄清楚三个问题。

Ok.. I'm trying to figure out three problems.

  1. 在我无法作出AlarmManager通知我
  2. 我要显示不同的消息为每个通知

我会很高兴,如果有人能帮助我,我不是这个张贴在这里,这样我可以得到code,我已经在这2天,所以我想我会请专家在这里

I will be glad if anyone can help me out, I'm not posting this here just so I can get the code, I have been at it for 2 days so I thought I'd ask the experts here.

如果我能找到谁就会检查低于code,显示我的过错,并指出我的方向来纠正这些错误。谢谢伊乌。

If I can find someone who would check the below code, show my wrongs and point me in the direction to rectify those wrongs.. Thank yhu.

下面是我设置的日期和时间,我想alarmManager发送报警我通知程序类。

Here is my Notifier class where I set the days and time I want the alarmManager to send an alarm.

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

}
}

和这里是我的Alarmservice建立通知者(只有一个通知在这里),我不知道如何设置另一个通知在未来三年报警。

and here is my Alarmservice to set up the notifiers (There is just one notifier here) I do not know how to set up another notifier for the next three alarms.

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为每个报警。 也,为了避免机器人优化它,一个不同的数据添加到意图

use a different myIntent for each alarm. also, to avoid android optimizing it, add a different data to the intent

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

这篇关于不能让AlarmManager和多通知工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆