简体   繁体   中英

Android How to show alert on another app

Hi I want to show alert after click on my notification. Here is code:

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

And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?

I know i must add

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

but nothing else..

Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your 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());

This code might help you, but still it depends what do you mean by "alert".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM