目前,我正在开发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" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
什么是倒计时器?
倒计时器可以理解为一种具有方法的迭代
onTick()和onFinish()
onTick()在CountDownTimer构造函数中给出的间隔(持续时间)之后被多次调用.
当longTimeInFuture已经存在时,onFinish()最后被调用(只有一次).