安卓java+弹出通知,其他活动之上的Android消息通知对话框

本文介绍了一种在Android应用中实现跨应用消息通知的方法。通过使用广播接收器和自定义的消息框活动,可以在接收到消息时显示一个对话框,即使当前不在应用内也能提醒用户查看消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值