Android通知问题

时间:2011-03-21 13:10:57

标签: android android-activity notifications broadcastreceiver alarmmanager

我使用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");
    }


}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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的构造需要花费很长时间(使用您的数据库访问权限),在这种情况下,您应该从{{1}生成IntentService做这项工作。

希望有帮助!

答案 2 :(得分:1)

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

考虑使用buzzbox api让您的生活更轻松。 您必须创建一个Task并将您的DB代码放在doWork方法中。 然后,您可以使用cron字符串安排任务..

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