英文:
How to keep broadcast receiver running in background?
问题
我有一个广播接收器。但当活动关闭时它被销毁....如何让它在后台运行。我知道可以通过使用服务来实现这一点...但如何在服务中实现它?
onStart:
@Override
protected void onStart() {
super.onStart();
private final BroadcastReceiver broadcast = new Broadcast();
IntentFilter filter = new IntentFilter("Update Player");
registerReceiver(broadcast, filter);
isReceiverRegistered = true;
}
Broadcast:
// BroadcastReceiver
public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase("Update Player")) {
position = intent.getIntExtra("position_2", -1);
setTextView();
byte[] image = getArt(PlayingScreen_List.get(position).getPath());
if (image != null) {
Glide.with(getApplicationContext()).asBitmap()
.load(image)
.into(profile_image);
} else {
Glide.with(getApplicationContext()).asBitmap()
.load(R.drawable.allsongs)
.into(profile_image);
}
}
}
}
OnPause:
@Override
protected void onPause() {
super.onPause();
if (isReceiverRegistered) {
unregisterReceiver(broadcast);
isReceiverRegistered = false;
}
}
英文:
I have a Broadcast Receiver. But it destroys when the activity closes....how do I keep it running in the background. I know I can do that by using a SERVICE...But how do I implement that in a service??
onStart:
@Override
protected void onStart() {
super.onStart();
private final BroadcastReceiver broadcast = new Broadcast();
IntentFilter filter = new IntentFilter("Update Player");
registerReceiver(broadcast, filter);
isReceiverRegistered = true;
}
Broadcast:
///BroadcastReceiver
public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase("Update Player")) {
position = intent.getIntExtra("position_2", -1);
setTextView();
byte[] image = getArt(PlayingScreen_List.get(position).getPath());
if (image != null) {
Glide.with(getApplicationContext()).asBitmap()
.load(image)
.into(profile_image);
} else {
Glide.with(getApplicationContext()).asBitmap()
.load(R.drawable.allsongs)
.into(profile_image);
}
}
}
}
OnPause:
@Override
protected void onPause() {
super.onPause();
if (isReceiverRegistered) {
unregisterReceiver(broadcast);
isReceiverRegistered = false;
}
}
答案1
得分: 1
你需要创建一个继承自 Service
的类,并在其中添加一个继承自 BroadcastReceiver
的内部类。请查看以下代码:
public class MyService extends Service {
BroadcastReceiver broadcastReceiver;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 做一些操作
}
// 构造函数
public MyReceiver(){
}
}
@Override
public void onCreate() {
// 创建 IntentFilter
IntentFilter intentFilter = new IntentFilter();
// 添加动作
intentFilter.addAction("com.example.NEW_INTENT");
intentFilter.addAction("com.example.CUSTOM_INTENT");
intentFilter.addAction("com.example.FINAL_INTENT");
// 创建并注册接收器
broadcastReceiver = new MyReceiver();
registerReceiver(broadcastReceiver, intentFilter);
}
}
在你的清单文件中添加以下代码:
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.example.NEW_INTENT"></action>
<action android:name="com.example.CUSTOM_INTENT"></action>
<action android:name="com.example.FINAL_INTENT"></action>
</intent-filter>
</receiver>
英文:
You need to create class which extends to Service
. Add inner class to it which extends to BroadcastReceiver
. Check out this code.
public class MyService extends Service {
BroadcastReceiver broadcastReceiver;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// do something
}
// constructor
public MyReceiver(){
}
}
@Override
public void onCreate() {
// create IntentFilter
IntentFilter intentFilter = new IntentFilter();
//add actions
intentFilter .addAction("com.example.NEW_INTENT");
intentFilter .addAction("com.example.CUSTOM_INTENT");
intentFilter .addAction("com.example.FINAL_INTENT");
//create and register receiver
broadcastReceiver = new MyReceiver();
registerReceiver(broadcastReceiver, intentFilter );
}
}
In your manifest add this code:
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.example.NEW_INTENT">
</action>
<action android:name="com.example.CUSTOM_INTENT">
</action>
<action android:name="com.example.FINAL_INTENT">
</action>
</intent-filter>
</receiver>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论