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

您的 ScheduleAlert 真的必须是一项活动吗?服务不会更好吗?Service 类不提供任何 GUI,因此不会出现黑屏。

于 2011-03-21T13:18:42.063 回答
1

您的代码显示一个空白屏幕,因为您在警报触发时启动了一个 Activity,并且没有 contentView 的 Activity 仍将占据全屏,但它将是空白的。您应该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预计构建需要很长时间(通过您的数据库访问),在这种情况下,您应该生成一个IntentServicefromonReceive()来完成工作。

希望有帮助!

于 2011-03-21T14:12:53.837 回答
1

您不能从 onReceive 方法开始您的活动。您必须再次使用通知管理器并创建另一个 PendingIntent 来描述当用户单击通知时会发生什么。

考虑使用 Buzzbox API 让您的生活更轻松。您必须创建一个任务并将您的数据库代码放入 doWork 方法中。然后您可以使用 cron 字符串安排您的任务。

更多信息: http ://hub.buzzbox.com/android-sdk/

于 2011-03-21T17:13:30.683 回答