从AppWidgetProvider或Service类中的app小部件创建新的Android Notification?

时间:2016-02-01 11:27:41

标签: android notifications widget appwidgetprovider

问:是否可以从应用小部件创建系统通知(显示在下拉阴影中)?

具体来说,我想从我这样做:
AppWidgetProvider.class,提取我的远程数据...... OR
{{1}},用于更新小部件UI。

1 个答案:

答案 0 :(得分:1)

是的,可以从服务创建通知。 这是一个示例代码。

private void triggerNotification() {
        CharSequence title = "title";
        CharSequence message = "message";
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification;
        notification = new Notification(
                R.drawable.ico_event, "Notifiy.. ",
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), title, message, pendingIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(1, notification);
    }

请注意,这是我从我的应用中获取的代码,因此您可能需要替换一些值以匹配您的应用。