使用接收器创建重复通知(BroadcastReceiver)

时间:2015-03-31 12:19:56

标签: android broadcastreceiver alarmmanager android-notifications

我创建了一个扩展BroadcastReceiver的接收器。这用于执行我的每日重复的通知,具体取决于用户选择的时间。由于某种原因,我不能让这个工作。我担心我从数字选择器错误的时间,但我是编程的新手,可以使用一些帮助。先感谢您。如果您发现任何错误,请告诉我。

这是我的主要活动(MyActivity)

公共类MyActivity扩展了Activity {

TimePicker timePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent pendingIntent;
int AM_PM;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    timePicker = (TimePicker) findViewById(R.id.timePicker);
    setAlarm = (Button) findViewById(R.id.setUpAlarm);

    setAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setAlarm();
        }
    });



}

private void setAlarm() {


    int hour = timePicker.getCurrentHour();
    int minute = timePicker.getCurrentMinute();
    long time = 60 * hour + minute;



    AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, NotifyService.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    //create alarms
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);


    // AlarmManager.INTERVAL_DAY.
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, alarmIntent);

}

这是我的Notification类(扩展BroadcatsReceiver)

public class NotifyService  extends BroadcastReceiver {
public NotifyService() {
}

@Override
public void onReceive(Context context, Intent intent) {



        //generate notification                                                     // should be splash activity
        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);
        Notification notification = new NotificationCompat.Builder(context)

                .setContentTitle(null)
                .setContentText("Your tip for today is ready")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pi)
                .build();

    }

// dont forget to compile "com.android.support:support-v4:18.0.+"



}

这是我的清单

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET" />





<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".NotifyService"
        android:enabled="true"
        android:exported="true" >
    </receiver>

</application>

1 个答案:

答案 0 :(得分:0)

请勿在Android清单中使用带过滤器的接收器。创建“显式意图”而不是使用意图过滤器。这样,

Intent i = new Intent(你的随机意图动作名称)。

这应该可以解决您的问题