繁体   English   中英

Android AlarmManager随机安排通知吗?

[英]Android AlarmManager Randomly Scheduling Notifications?

我正在尝试创建一个提醒应用程序,该应用程序会在状态栏中弹出一个提醒。 据我了解,Android NotificationManager是用于调度通知的标准工具,但是需要BOOTService来重新调度通知,因为调度的事件无法在启动时继续存在。

我拼凑了一个应用程序,该应用程序在启动时安排一个提醒,并在状态栏中弹出一个通知。 如果您单击该通知,它将启动MainActivity,将来它将提供添加,删除或其他提醒的选项。

我遇到的问题是,提醒功能在第一次就可以正常运行,但是由于某种原因,它似乎重新安排了时间,并在随机时间再次弹出。 我应该启动另一个活动,而不是安装BootAlarmService的活动吗?

更新:所以我认为我发现了有关logcat的一些线索。 显然,服务正在崩溃并正在重新启动,这正在重置通知。 任何想法为什么会这样?

更新二:代码已更改为工作模型

ActivityManager   I  No longer want com.example.alarm_boot_test (pid 1428): hidden #16
ActivityManager   W  Scheduling restart of crashed service com.example.alarm_boot_test/.BootAlarmService in 5000ms
ActivityManager   W  Scheduling restart of crashed service com.example.alarm_boot_test/.NotificationAlarmService in 15000ms
ActivityManager   I  Start proc com.example.alarm_boot_test for service com.example.alarm_boot_test/.BootAlarmService: pid=2321 uid=10069 gids={}
       dalvikvm   D  Debugger has detached; object registry had 1 entries
        szipinf   D  Initializing inflate state
BootAlarmService  D  oncreate()
BootAlarmService  D  alarm set for Thu Jan 17 08:03:00 CST 2013

MainActivity.java

package com.example.alarm_boot_test;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{

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


}

BootAlarmService.java

package com.example.alarm_boot_test;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BootAlarmService extends Service
{
    private final String TAG = this.getClass ().getName ();

    @Override
    public void onCreate ()
    {
        Log.d (TAG, "oncreate()");
        super.onCreate ();
    }


   @Override
   public int onStartCommand (Intent intent, int flags, int startId)
   {
       Log.d (TAG, "alarm_test: BootAlarmService.onStartCommand() Received start id " + startId + ": " + intent);

       // if intent == null, service has been killed/restarted by system
       if (intent != null)
        createNotificationOnBoot();
       else
        Toast.makeText (getBaseContext (), "Intent was null in BootAlarmService.", Toast.LENGTH_LONG).show();


       return START_STICKY;
    }


    private void createNotificationOnBoot ()
    {
        Intent inotify = new Intent(this , NotificationAlarmService.class);
        inotify.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager amgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, inotify, 0);

        // go off two mins from now
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, calendar.get (Calendar.MINUTE) + 2);

        amgr.set (AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis (), pendingIntent);


        Log.d (TAG, "alarm set for " + calendar.getTime ().toString ());
    }


    @Override
    public IBinder onBind (Intent intent)
    {
        return null;
    }


}

BootReceiver.java

package com.example.alarm_boot_test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive (final Context context, final Intent bootintent)
    {
        Intent i = new Intent ();
        i.setAction ("com.example.alarm_boot_test.BootAlarmService");
        context.startService (i);
    }

}

NotificationAlarmService.java

package com.example.alarm_boot_test;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class NotificationAlarmService extends Service
{
    private final String TAG = this.getClass().getName ();


    @Override
    public void onCreate ()
    {
        super.onCreate ();
    }


    @Override
    public int onStartCommand (Intent intent, int flags, int startId)
    {
       Log.d (TAG, "alarm_test: NotificationAlarmService.onStartCommand()");
       if (intent != null)
          createNotification ();
       else
          Toast.makeText (getBaseContext (), "Intent was null in NotificationAlarmService.", Toast.LENGTH_LONG).show();

       return super.onStartCommand (intent, flags, startId);
     }



    private void createNotification()
    {
      NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      Notification notification = new Notification(android.R.drawable.stat_sys_warning, "Note from AlarmService", System.currentTimeMillis());
      Intent i = new Intent(this, ViewReminder.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

      notification.setLatestEventInfo(this, "New Notification", "You have been notified by AlarmService", pendingIntent);
      notificationManager.notify(10001, notification);

   }




    @Override
   public IBinder onBind (Intent intent)
   {
       return null;
   }
}

* activity_main.xml中*

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Service started! Reboot!" />

</RelativeLayout>

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarm_boot_test"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service android:name="com.example.alarm_boot_test.BootAlarmService" >
            <intent-filter>
                <action android:name="com.example.alarm_boot_test.BootAlarmService" >
                </action>
            </intent-filter>
        </service>

        <receiver android:name="com.example.alarm_boot_test.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>

        <service android:name="com.example.alarm_boot_test.NotificationAlarmService" >
        </service>

        <activity
            android:name="com.example.alarm_boot_test.MainActivity"
            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>

</manifest>

如果它对其他人有帮助,我想我需要在BootAlarmService.java和NotificationAlarmService.java中的onStartCommand()方法中检查传递的NULL意向。 仅测试了几天,但如果系统终止/重启服务,则Intent为NULL。 对此进行简单的测试就可以使我仅在引导时启动服务时(当传递的Intent不为null时)创建通知。

暂无
暂无

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

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