【问题标题】:Android Notification problem安卓通知问题
【发布时间】:2011-03-21 13:10:57
【问题描述】:

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


}

【问题讨论】:

    标签: android android-activity notifications broadcastreceiver alarmmanager


    【解决方案1】:

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

    【讨论】:

    • 是否可以访问任何数据库或在服务中创建通知?
    • 是的,服务也是一个上下文,你可以在没有 gui 的情况下做同样的事情
    • 如果您要实现自己的自定义服务(矫枉过正),请确保在工作完成后让服务自行停止,否则它将继续运行并且可能不会以相同的方式出现您希望第二次调用它...或使用IntentService 为您处理其中的一些问题。
    【解决方案2】:

    您的代码显示空白屏幕,因为您启动了一个 Activity 作为警报触发的结果,并且没有 contentView 的 Activity 仍将占据全屏,但它将是空白的。您应该直接在BroadcastReceiver 中构建和触发您的Notification,而不是生成其他系统组件。

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

    请注意,在调用此方法时会为您提供上下文,因此您不必仅仅为了获得访问权限而生成 Activity 或 Service。

    唯一的例外是如果Notification 的构建预计需要很长时间(通过您的数据库访问),在这种情况下,您应该从onReceive() 生成一个IntentService 来完成这项工作。

    希望有帮助!

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-20
        • 2017-12-01
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多