1

このコードを使用して、alarmmanager を 2012 年 12 月 25 日 - 15.15 に Notify.class に設定しようとしましたが、このコードを使用すると Notify.class が開始されません。問題はどこですか?

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());

    cal.set(Calendar.DATE,25); 
    cal.set(Calendar.MONTH,Calendar.DECEMBER);
    cal.set(Calendar.YEAR,2012);
    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.MINUTE, 15);     
    cal.set(Calendar.SECOND, 00);     

Intent intent3 = new Intent(context, Notify.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent3, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

マニフェスト:

<activity android:name="Notify"></activity>
<receiver android:name="AlarmReceiver" >
            <intent-filter>
                <action android:name="com.example.AlarmReceiver" />
            </intent-filter>
4

2 に答える 2

1

あなたはこれを書くべきです

<receiver android:name="com.example.AlarmReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="AlarmReceiver"/>
        </intent-filter>
</receiver>

レシーバーのandroid:nameを切り替えましたが、これはインテントフィルターです。com.example.AlarmReceiver.AlarmReceiverに置き換えることができます

マニフェストにこの変更を加えた後、ブロードキャストレシーバーcom.example.AlarmReceiverのonReceive()メソッド内に記述したコードが実行されます。

コードが行うことは、指定された日時にアラームマネージャーを設定することです。その瞬間、アクション名AlarmReceiverでインテントを起動します。ブロードキャストレシーバーはマニフェストにあるため、ブロードキャストをキャッチしてonReceive()メソッドを実行します。

アクティビティを開始する場合は、onReceive()内にcontext.startActivity(...)を記述します。

編集:また、これにあなたの意図を変更します:

Intent intent3 = new Intent("AlarmReceiver");
于 2012-12-23T16:28:47.210 に答える
1

Activity Notify.class を開始しようとしているだけの場合は、次の行を変更するだけです。

PendingIntent.getBroadcast

PendingIntent.getActivity

また、マニフェストで次の行を変更する必要があります。

から:

<activity android:name="Notify"></activity>

<activity android:name=".Notify"></activity>

このアクティビティには、android:name と android:label も指定する必要があります。

代わりに getBroadcast を使用しようとしている場合は、保留中のインテントでアクティビティを開始する代わりに、レシーバーがブロードキャストを取得するように、この行を変更する必要があります。そのために使用します

Intent intent3 = new Intent(context, AlarmReceiver.class);

それ以外の

Intent intent3 = new Intent(context, Notify.class);

インテント 3 にアクションを追加することもできます。例:

Intent3.setAction("com.example.AlarmReceiver");

そうすれば、AlarmReceiver でそのインテントをフィルタリングし、新しいインテントを使用してアクティビティ Notify.class を起動するなどのアクションを実行できます。

于 2012-12-23T16:22:47.987 に答える