Android如何在其他应用上显示提醒

时间:2014-04-15 20:22:37

标签: java android alert android-notifications

您好我想在点击我的通知后显示提醒。这是代码:

@SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());

 notification.ledARGB = Color.CYAN;//0xFFff0000;
 notification.ledOnMS = 800; 
 notification.ledOffMS = 2400;

 notification.vibrate = new long[]{100, 120, 100, 120};

 Intent notificationIntent = new Intent(this, TerminarzActivity.class);

 notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

 notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
 notificationManager.notify(1, notification);
}

我希望在点击后显示任何不在我的活动上的警报。有谁知道怎么做?

我知道我必须添加

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

但没有别的..

1 个答案:

答案 0 :(得分:1)

查看此通知的创建(取自Geofence示例)。此代码会创建一个通知,如果您触摸它,则会启动您的MainActivity。

    // Create an explicit content Intent that starts the main Activity
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Adds the main Activity to the task stack as the parent
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions
    // >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Set the notification contents
    builder.setSmallIcon(R.drawable.ic_folder)
            .setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());

此代码可能对您有所帮助,但仍取决于您的意思&#34; alert&#34;。