gpt4 book ai didi

android - 关闭应用程序后,广播接收器无法在 Oppo、Mi 和其他一些手机中工作

转载 作者:太空狗 更新时间:2023-10-29 14:41:02 25 4
gpt4 key购买 nike

我正在尝试检测传入的短信,为此我创建了广播接收器来收听传入的短信。当应用程序位于前台时,它可以完美运行。但是当我从最近的应用程序中关闭应用程序时,然后在 Oppo 和 xiaomi 等手机上,Receiver 停止工作并被杀死.

下面是我实现的代码:

SmsReceiver.java

   public class SmsReceiver extends BroadcastReceiver {

SmsMessage currentMessage;

public SmsReceiver() {
}

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

Toast.makeText(context, " reciever called ", Toast.LENGTH_SHORT).show();

Bundle data = intent.getExtras();

Object[] pdus = (Object[]) data.get("pdus");
for (int i = 0; i < pdus.length; i++) {
//SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
} else {
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
}

String sender = currentMessage.getDisplayOriginatingAddress();

if (sender.contains("HP-CRAZND")) {

String messageBody = currentMessage.getMessageBody();

if (messageBody.toString() != null || !messageBody.toString().isEmpty()) {
Intent ii = new Intent(context, MyService.class);

ii.putExtra("s", sender);
ii.putExtra("m", messageBody);
context.startService(ii);
}
}
}


}

}

AndroidManifest.xml:

我已经添加了接收器如下

           <receiver
android:name="saiinfotech.sms.SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

最佳答案

enable app in autostart then its working fine 

using kotlin, here two extension function first one get manufacturing detail
second one is to open setting to auto start app

//call this method to get manufacturing detail of mobile
fun Context.checkManufacturing(): Intent? {
var intent:Intent?=null
try {
intent=Intent()
val manufacturer = android.os.Build.MANUFACTURER
if ("xiaomi".equals(manufacturer, ignoreCase = true)) {
intent?.component = ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return intent
} else if ("oppo".equals(manufacturer, ignoreCase = true)) {
intent?.component = ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return intent
} else if ("vivo".equals(manufacturer, ignoreCase = true)) {
intent?.component = ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return intent
}

return null
} catch (e: Exception) {
e.message!!.toast()
return null

}
return null
}

/// pass intent here get fron check manufacturing method to open setting

fun Intent.autostart(context: Context){
context.startActivity(this)
}

关于android - 关闭应用程序后,广播接收器无法在 Oppo、Mi 和其他一些手机中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075045/

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