其他分享
首页 > 其他分享> > 使用AlarmManager设置重复通知-Android

使用AlarmManager设置重复通知-Android

作者:互联网

我正在使用TimePicker从用户那里获取特定时间.然后,我每天都在这段时间使用此时间设置重复警报.当警报响起时,我希望将通知发送给用户.我的代码似乎是正确的,并且我在android studio中没有收到任何错误,但是当我运行此应用并在特定时间设置它时…它永远不会关闭.请帮忙.另外我还找不到任何能显示如何使用TimePicker获得AM或PM用户选择的东西.我的代码如下.提前致谢.

这是MyActivity(启动时打开的那个)

public class MyActivity extends Activity {

TimePicker mTimePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent mPendingIntent;
int AM_PM;

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



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


    mTimePicker = (TimePicker) findViewById(R.id.timePicker);

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


            setAlarm();



        }
    });



}

private void setAlarm() {
    hour = mTimePicker.getCurrentHour();
    minute = mTimePicker.getCurrentMinute();

    Intent intent = new Intent(this, NotifyService.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    mPendingIntent = PendingIntent.getService(this, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND , 0 );
    calendar.set(Calendar.MINUTE , 0 + minute);
    calendar.set(Calendar.HOUR , 0 + hour);
    calendar.set(Calendar.AM_PM , Calendar.PM);


    Toast.makeText(this, calendar.get(Calendar.MINUTE) + "    " + calendar.get(Calendar.HOUR), Toast.LENGTH_SHORT).show();

    //  * 60 * 60 * 24

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP , calendar.getTimeInMillis() , 1000 * 60 * 60 * 24 , mPendingIntent);

    // Toast.makeText(MyActivity.this , "Alarm Set" , Toast.LENGTH_SHORT).show();
}

这是我的通知课

public class NotifyService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {

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

    NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this.getApplicationContext() , MyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent ,0 );

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("Come Back!")
            .setContentText("Have you seen todays tip?")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .setSound(sound)
            .build();

    mNM.notify( 1 , mNotify);

}

}

我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidy.notificationapp" >

<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>
</application>

解决方法:

您必须在清单中声明您的服务,否则它将无法启动.

同样,getCurrentHour()始终以24h格式返回小时,无需知道用户输入的是AM还是PM.

http://developer.android.com/reference/android/widget/TimePicker.html#setCurrentHour(java.lang.Integer)

您可能还需要考虑将Service中的所有工作从onCreate()移到onStartCommand(),因为仅当Service不存在时才调用onCreate(),这可能是因为您似乎没有停止而已它(您也可以考虑这样做).

http://developer.android.com/guide/components/services.html

标签:repeatingalarm,notifications,alarmmanager,android
来源: https://codeday.me/bug/20191120/2044800.html