安卓通知问题

发布于 2024-10-25 13:18:27 字数 2594 浏览 5 评论 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");
    }


}

I have created a Alarm using 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);

Here is NotificationMessage class.

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

It is calling a Intent for creating a Notification. For getting Notification text, I have to access the database. I want to create sound and vibration for notification. And also show the notification icon at top bar, but no view. But it is showing a black screen at the time of notification. How to solve it?

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-11-01 13:18:27

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

Does your ScheduleAlert really have to be an activity? Wont a Service not be better? The Service class does not provide any GUI so no black screen.

九命猫 2024-11-01 13:18:27

您的代码显示空白屏幕,因为您由于警报触发而启动了一个活动,并且没有 contentView 的活动仍将占据全屏,但它将是空白的。您应该直接在 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");
    }
}

请注意,调用此方法时,您将获得一个上下文,因此您不必仅仅为了获得对某个活动或服务的访问权限而生成一个活动或服务。

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

希望有帮助!

Your code is showing a blank screen because you launched an Activity as a result of the alarm firing, and an Activity without a contentView will still take the full screen, but it will be blank. You should be constructing and firing your Notification directly in the BroadcastReceiver, not spawning other system components.

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

Notice that you are provided with a context when this method is called, so you don't have to spawn an Activity or Service just for the purposes of gaining access to one.

The ONLY exception to this is if the construction of the Notification is expected to take a long time (with your DB access), in which case you should spawn an IntentService from onReceive() to do the work.

Hope that Helps!

心碎的声音 2024-11-01 13:18:27

您无法从 onReceive 方法启动您的活动。您必须再次使用通知管理器并创建另一个 PendingIntent 来描述用户单击通知时将发生的情况。

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

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

You can't start your activity from the onReceive method. You have to use again the notification manager and create another PendingIntent that describes what is gonna happen when the user click on the Notification.

Consider using the buzzbox api to make your life easier.
You have to create a Task and putting your DB code in the doWork method.
Then you can schedule your Task using a cron string..

More info: http://hub.buzzbox.com/android-sdk/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文