【问题标题】:Setting a repeating Notification with an AlarmManager - Android使用 AlarmManager 设置重复通知 - Android
【发布时间】:2015-06-04 01:19:33
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: android notifications alarmmanager repeatingalarm


    【解决方案1】:

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

    此外,getCurrentHour() 总是以 24 小时格式返回小时,无需知道用户输入的是上午还是下午。

    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

    【讨论】:

    • 我会在清单中声明我的服务吗,例如 @ci_
    • 如果我将我的服务移动到 onStartComand 我会返回什么?
    • 您返回此处定义的常量之一 developer.android.com/reference/android/app/…, int, int) 或返回对 super 的默认调用。如果您点击答案中的链接,它还将显示如何在清单中声明服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多