我需要什么
I want to detect the incoming calls even my app is removed from the
@H_419_8@
recent apps List.Manifest.xml代码
<receiver androID:name=".PhonestateReceiver"> <intent-filter> <action androID:name="androID.intent.action.PHONE_STATE" /> </intent-filter> </receiver>我的广播接收器代码
@OverrIDepublic voID onReceive(Context context,Intent intent) { //my call blocking code}我的问题
My broadcastReceiver wont work in the background as if I removed from
@H_419_8@
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 – 如何保持活着我的BroadcastReceiver全部内容,希望文章能够帮你解决android – 如何保持活着我的BroadcastReceiver所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)