How do I get an Android message notification dialog on top of other activities?
I am searching for a solution to this problem and still not yet got the solution. Right now, I am developing a social networking app in which I need to show a notification message dialog whenever the user gets some message from another and to achieve that I have used the broadcast receiver and it is working fine.
The problem is how to show the notification dialog on top of another application.
Yes, it is possible. The Main.xml layout has one edit text and button. The Messagebox layout has one button. Here you can change message layout to whatever you want.
File MyScheduledReceiver.java:
public class MyScheduledReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent scheduledIntent = new Intent(context, MessageBox.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
}
Main Activity:
public class AndroidMessageBoxActivity extends Activity implements OnClickListener
{
private EditText time;
private Button btn;
private AlarmManager alarm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
time = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int x = Integer.parseInt(time.getText().toString());
Intent intent = new Intent(this, MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (x * 1000),
pendingIntent);
Toast.makeText(this,
"Alarm set in " + x + " seconds",
Toast.LENGTH_LONG).show();
}
}
MessageBox:
public class MessageBox extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messagebox);
Button btn = (Button) findViewById(R.id.Ok);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
And add these two lines in the Android manifest XML file:
<receiver android:name="MyScheduledReceiver"></receiver>
<activity android:name="MessageBox" android:theme="@style/Theme.Transparent"></activity>
Filestyle.xml:
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
As others have said in their responses, launching an Activity from the background in order to get the user's attention is discouraged. However, some situations call for this; examples in Android itself include clock alarms, incoming calls, and low battery alerts.
In Gingerbread the fullScreenIntent field was added to Notification objects; this is a standard and convenient way to post a Notification that also launches an Activity to really get the user's attention. As of Gingerbread, the platform components I listed above (alarms, etc.) all use this technique to show their alerts. This is the recommended way to do what you're asking.
As stated in the documentation on Status bar notifications:
A background service should never launch an activity on its own in order to receive user interaction.
Therefore, I strongly advice you against doing that, but you should use a status bar notification instead.
来源:https://stackoverflow.com/questions/8267130/android-message-notification-dialog-on-top-of-other-activities