0

AlarmManager を使用してアラームを作成しました。

Intent intent = new Intent(MyApp.this,NotificationMessage.class);
PendingIntent sender = PendingIntent.getBroadcast(MyApp.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime,sender);

これが NotificationMessage クラスです。

public class NotificationMessage extends BroadcastReceiver {
    // Display an alert that we've received a message.
    // @Override
    public void onReceive(Context context, Intent intent) {


        Intent myIntent = new Intent();
        myIntent.setClass(context, ScheduleAlert.class);
        myIntent.setAction(ScheduleAlert.class.getName());
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(myIntent);
    }
}

通知を作成するための Intent を呼び出しています。通知テキストを取得するには、データベースにアクセスする必要があります。音とバイブレーションで通知したい。また、トップバーに通知アイコンを表示しますが、表示されません。しかし、通知時に黒い画面が表示されます。それを解決する方法は?

public class ScheduleAlert extends Activity {

    private String notificationAlart;

    // ************* Notification and AlarmManager ************//

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


                // get contentText from the database


        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                final Notification notifyDetails = new Notification(
                        R.drawable.icon, "MyApp", nextAlarmTime);

                Context context = getApplicationContext();
                CharSequence contentTitle = "MyApp";
                CharSequence contentText = notificationAlart;

                Intent notifyIntent = new Intent(context, MyApp.class);

                PendingIntent pendingIntent = PendingIntent.getActivity(
                        ScheduleAlert.this, 0, notifyIntent,
                        android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
                notifyDetails.setLatestEventInfo(context, contentTitle,
                        contentText, pendingIntent);


                notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
                notifyDetails.defaults |= Notification.DEFAULT_SOUND
                        | Notification.DEFAULT_VIBRATE;
                mNotificationManager.notify((int) editEventid, notifyDetails);
                Log.d(null,"notification set");
    }


}
4

3 に答える 3

1

アラームが発生した結果としてアクティビティを起動したため、コードは空白の画面を表示しています。contentView のないアクティビティは引き続き全画面表示になりますが、空白になります。他のシステム コンポーネントを生成Notificationするのではなく、 で直接構築して起動する必要があります。BroadcastReceiver

public class NotificationMessage extends BroadcastReceiver {
    // Display an alert that we've received a message.

    public void onReceive(Context context, Intent intent) {
        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
        Notification notifyDetails = new Notification(R.drawable.icon, "MyApp", nextAlarmTime);

        CharSequence contentTitle = "MyApp";
        CharSequence contentText = notificationAlart;

        Intent notifyIntent = new Intent(context, MyApp.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(
                ScheduleAlert.this, 0, notifyIntent,
                android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notifyDetails.setLatestEventInfo(context, contentTitle,
                contentText, pendingIntent);


        notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
        notifyDetails.defaults |= Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify((int) editEventid, notifyDetails);
        Log.d(null,"notification set");
    }
}

このメソッドが呼び出されるとコンテキストが提供されるため、アクティビティまたはサービスにアクセスするためだけにアクティビティまたはサービスを生成する必要がないことに注意してください。

これに対する唯一の例外は、 の構築にNotification(DB アクセスで) 長い時間がかかることが予想される場合です。この場合、作業を行うためにIntentServicefromを生成する必要があります。onReceive()

それが役立つことを願っています!

于 2011-03-21T14:12:53.837 に答える
1

onReceive メソッドからアクティビティを開始することはできません。通知マネージャーを再度使用して、ユーザーが通知をクリックしたときに何が起こるかを説明する別の PendingIntent を作成する必要があります。

生活を楽にするためにバズボックス API の使用を検討してください。Task を作成し、DB コードを doWork メソッドに入れる必要があります。次に、cron 文字列を使用してタスクをスケジュールできます。

詳細: http://hub.buzzbox.com/android-sdk/

于 2011-03-21T17:13:30.683 に答える
1

あなたの ScheduleAlert は本当にアクティビティである必要がありますか? サービスが良くなりませんか? Service クラスは GUI を提供しないため、黒い画面はありません。

于 2011-03-21T13:18:42.063 に答える