2

ステータス バーにリマインダーをポップアップ表示するリマインダー アプリケーションを作成しようとしています。私が理解しているように、Android NotificationManager は通知をスケジュールするために使用する標準ツールですが、スケジュールされたイベントは起動後に存続しないため、通知を再スケジュールするには BootService が必要です。

以下に、起動時に単一のリマインダーをスケジュールし、ステータス バーに通知をポップアップ表示するアプリケーションをまとめました。通知をクリックすると、MainActivity が起動します。将来的には、リマインダーを追加したり削除したりできるオプションが追加される予定です。

私が抱えている問題は、リマインダーが最初は正しく機能することですが、それ自体が再スケジュールされ、何らかの理由でランダムな時間に再びポップアップしているようです. BootAlarmService をインストールするアクティビティではなく、別のアクティビティを起動する必要がありますか?

更新: logcat でいくつかの手がかりを見つけたと思います。どうやらサービスがクラッシュして再起動され、通知がリセットされているようです。これはなぜですか?

UPDATE II :コードを作業モデルに変更

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

*アクティビティ_メイン.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>
4

1 に答える 1

1

他の誰かに役立つ場合は、BootAlarmService.java と NotificationAlarmService.java の両方で、onStartCommand() メソッドで渡された NULL のインテントを確認する必要があると思います。数日しかテストしていませんが、システムがサービスを強制終了/再起動すると、インテントが NULL のように見えます。これをテストするだけで、起動時にサービスが開始されたとき (渡された Intent が null でないとき) にのみ通知を作成できました。

于 2013-01-23T15:59:15.610 に答える