如何使广播接收器在后台保持运行?

huangapple go评论86阅读模式
英文:

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(&quot;com.example.NEW_INTENT&quot;);
         intentFilter .addAction(&quot;com.example.CUSTOM_INTENT&quot;);
         intentFilter .addAction(&quot;com.example.FINAL_INTENT&quot;);

         //create and register receiver
         broadcastReceiver = new MyReceiver();
         registerReceiver(broadcastReceiver, intentFilter );
    }
}

In your manifest add this code:

&lt;receiver android:name=&quot;MyReceiver&quot;&gt;
     &lt;intent-filter&gt;
        &lt;action android:name=&quot;com.example.NEW_INTENT&quot;&gt;
        &lt;/action&gt;
        &lt;action android:name=&quot;com.example.CUSTOM_INTENT&quot;&gt;
        &lt;/action&gt;
        &lt;action android:name=&quot;com.example.FINAL_INTENT&quot;&gt;
        &lt;/action&gt;
     &lt;/intent-filter&gt;
&lt;/receiver&gt;

huangapple
  • 本文由 发表于 2020年8月3日 23:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63232143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定