我正在做一个Android应用程序,我想从活动中获取数据到服务 然后这个服务会发出通知并将数据发送到另一个活动,用户可以在活动中看到结果吗?请帮我
答案 0 :(得分:0)
将此内容写入您要发送数据的活动中:( msg是您要发送的数据)
Intent myintent = new Intent("package name of class where you want to send");
myintent.putExtra("message", msg);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//You might need this
getApplicationContext().sendBroadcast(myintent);
在接收者活动的onCreate()中: 添加:(设置内容视图后的任何地方)
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
在课堂上添加:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Data received is : " + intent.getStringExtra("message"));
//Make a Notification here
}
};
另外(在接收者活动中):
@Override
public void onResume() {
super.onResume();
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
}
protected void onPause() {
super.onPause();
getApplicationContext().unregisterReceiver(broadcastReceiver);
}