问题描述
如何在其他活动之上获得一个Android消息通知对话框?
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.
推荐答案
是的,有可能. Main.xml布局具有一个编辑文本和按钮.消息框布局具有一个按钮.在这里,您可以将邮件布局更改为所需的任何内容.
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.
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);
}
}
主要活动:
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();
}
});
}
}
并在Android清单XML文件中添加以下两行:
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>
这篇关于其他活动之上的Android消息通知对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!