简体   繁体   English

使用AlarmManager设置重复通知 - Android

[英]Setting a repeating Notification with an AlarmManager - Android

I am using a TimePicker to get a specific time from the user. 我正在使用TimePicker从用户那里获取特定时间。 I am then using this time to set a repeating alarm at this time every day. 我正在利用这段时间每天在这个时间设置重复闹钟。 When the alarm goes off i want a Notification to go be sent to user. 当闹钟响起时,我想要将通知发送给用户。 My code seems to be right and i am not getting any errors in android studio, but when i run this app and set it at a specific time...IT NEVER GOES OFF. 我的代码似乎是正确的,我没有在Android工作室中得到任何错误,但当我运行这个应用程序并在特定时间设置它...它永远不会关闭。 please help. 请帮忙。 Also i haven't been able to find anything that shows me how to get the AM or PM user selection with the TimePicker. 此外,我还没有找到任何能告诉我如何使用TimePicker获取AM或PM用户选择的内容。 My code is below. 我的代码如下。 Thanks in advance. 提前致谢。

Here is MyActivity ( the one that opens upon launch ) 这是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();
}

Here is my notification class 这是我的通知课程

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

}

} }

My manifest 我的清单

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

You have to declare your Service in the manifest or it can't be started. 您必须在清单中声明您的服务,否则无法启动它。

Also getCurrentHour() always returns the hour in 24h format, no need to know if the user entered in AM or PM. 此外,getCurrentHour()始终以24小时格式返回小时,无需知道用户是在AM还是PM中输入。

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

You also might want to consider to move all the work in your Service from onCreate() to onStartCommand() because onCreate() is only called if your Service doesn't exist yet, which it might, because you don't seem to stop it (which you might also consider doing). 您还可以考虑将服务中的所有工作从onCreate()移动到onStartCommand(),因为只有当您的服务尚不存在时才会调用onCreate(),因为您似乎不会停止它(你也可以考虑做)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM