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:
Filestyle.xml:
true
@null
@android:color/transparent
true
false
本文介绍了一种在Android应用中实现跨应用消息通知的方法。通过使用广播接收器和自定义的消息框活动,可以在接收到消息时显示一个对话框,即使当前不在应用内也能提醒用户查看消息。
1998

被折叠的 条评论
为什么被折叠?



