gpt4 book ai didi

android - 如何让我的广播接收器保持活力

转载 作者:IT王子 更新时间:2023-10-28 23:32:53 26 4
gpt4 key购买 nike

目前我正在开发像 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.

我的完整 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..但不适用于 Lollipop ..

更新问题:

如果无法让广播接收器保持 Activity 状态,即使我的应用已关闭,我如何才能检测到来电?

谁给个详细的回答..

最佳答案

这里我们正在通知服务接收者。

所以将服务类设为

    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();
}
}

现在从主要 Activity 启动服务

    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));

}
}

在manifest中声明receiver和service如下

 <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" />

将以下权限添加到您的 list 。这用于防止 cpu 休眠。

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

什么是倒计时?

倒计时可以理解为迭代,需要方法

onTick() 和 onFinish()

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

onFinish() 在 longTimeInFuture 到达时最后(仅一次)被调用。

关于android - 如何让我的广播接收器保持活力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com