gpt4 book ai didi

java - 检查服务是否正在从广播接收器内运行

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:27 25 4
gpt4 key购买 nike

我知道这和我的问题类似。 https://stackoverflow.com/a/22511290/2208342但我读到,随着 Android 删除该代码,该问题的解决方案将变得过时。

以下引用摘自https://stackoverflow.com/a/5921190/2208342

I'd discourage the use of this solution. For Android L they're removing ActivityManager.getRecentTasks() and it had the same note in the documentation. So be warned!

我还听说这个解决方案不适用于 android kitkat。

BroadCastReceiver 类

public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();


public IncomingSms(){}


@Override
public void onReceive(Context context, Intent intent) {

final Bundle bundle = intent.getExtras();

Intent intent1 = new Intent(context, CallDetectService.class);



if (context.stopService(intent1)) {
Toast.makeText(context,"if context",Toast.LENGTH_LONG).show();
try {


if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();


String sms = "TEST";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(senderNum, null, sms, null, null);

Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
}


Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);

// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show();

} // End For loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReciever", "Exeption smsReceiver" + e);
}
}
else{
Toast.makeText(context,"else context",Toast.LENGTH_LONG).show();
}

}
}

服务等级

public class CallDetectService extends Service {


private CallHelper callHelper;

public CallDetectService() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId){
callHelper = new CallHelper(this);


int res = super.onStartCommand(intent, flags, startId);
callHelper.start();
Toast.makeText(getApplicationContext(), "Starting Service", Toast.LENGTH_LONG).show();

return res;
}

@Override
public void onDestroy(){
super.onDestroy();


callHelper.stop();
//Toast.makeText(getApplicationContext(),"Stop Service",Toast.LENGTH_LONG).show();


}

@Override
public IBinder onBind (Intent intent) {
return null;
}
}

在我的 MainActivity 中,我有这段代码,这正是我在广播接收器中想要的代码。

Intent intent = new Intent(this,CallDetectService.class);

if (stopService(intent)){

最佳答案

有一个解决方法,但如果您在 Activity 中使用接收器:

  • 将接收器类添加为 Activity 中的内部类

    public final class MyActivity extends Activity {
    public final class IncomingSms extends BroadcastReceiver {
    ...
    }
  • 在 Activity 中动态注册/取消注册接收器onStart()/onStop()
  • 每当您绑定(bind)到此服务时,让您的 Activity 获取您的服务的引用
  • 在您的接收器onReceive()中使用引用

注意

请记住,上述实现存在一些缺陷:

  • 内部类可以显式访问任何外部类成员变量(字段和方法)
  • 这会导致潜在的内存泄漏,尤其是当您的服务在单独的线程中执行某些操作时
  • 当 Activity 存在时,您的接收器处于 Activity 状态

关于java - 检查服务是否正在从广播接收器内运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362774/

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