android – 如何保持活着我的BroadcastReceiver

目前,我正在开发Truecaller等阻塞应用程序.

我需要什么

I want to detect the incoming calls even my app is removed from the
recent apps list.

Manifest.xml代码

<receiver android:name=".PhonestateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

我的广播接收器代码

@Override
public void onReceive(Context context,Intent intent) {
  //my call blocking code
}

我的问题

My broadcastReceiver wont work in the background as if I removed from
the recent apps list.

我的完整清单代码在这里

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ranjith.callblocker">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.GET_TASKS" />

<application
    android:allowBackup="true"
    android:enabled="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver
        android:name=".PhonestateReceiver"
        android:enabled="true"
        android:exported="true"
        android:process=":anotherProcess">
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我应该使用服务还是别的什么?

更新:

有了Suraj的答案,我在接收器中尝试过这个标签

android:enabled="true"
    android:exported="true"
    android:process=":anotherProcess"

它适用于kitkat ..但不适用于棒棒糖..

更新问题:

Incase如果不可能保持活动广播接收器即使我的应用程序关闭,我如何检测来电?

任何人给出详细的答案

解决方法

在这里,我们通知接收方服务.

所以做一个服务类

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        new CountDownTimer(100000,4000)
        {
            @Override
            public void onTick(long millisUntilFinished) {
                sendbroadcast(new Intent("fromservice"));

            }

            @Override
            public void onFinish() {

            }
        }.start();
        return START_STICKY;
    }
}

现在做一个接收器

public class MyReceiver extends WakefulbroadcastReceiver {
        @Override
        public void onReceive(final Context context,Intent intent) {

            Toast.makeText(context,"inside receiver",Toast.LENGTH_SHORT).show();
        }
    }

现在从主要活动开始服务

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            startService(new Intent(this,MyService.class));

        }
    }

在清单中声明接收者和服务如下

<receiver android:name=".MyReceiver"
        android:process=":jk"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="fromservice"/>
        </intent-filter>
    </receiver>
    <service android:name=".MyService"
android:process=":ff"
android:enabled="true"
android:exported="true" />

将以下权限添加到清单中.
这用于防止cpu睡觉.

<uses-permission android:name="android.permission.WAKE_LOCK"/>

什么是倒计时器?

倒计时器可以理解为一种具有方法的迭代

onTick()和onFinish()

onTick()在CountDownTimer构造函数中给出的间隔(持续时间)之后被多次调用.

当longTimeInFuture已经存在时,onFinish()最后被调用(只有一次).

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...