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

时间:2018-01-03 09:58:46

标签: android broadcastreceiver

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

以下是我实施的代码:

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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

实际上,我认为这与制造商无关。一个人需要一项服务来在后台,正在进行的生存活动中销毁。

如果需要不时进行此操作,请将 Jobscheduler 与构建器一起使用,并设置“ Periodic”并保持重​​启。 setPersisted(true)

顺便说一下,小米和Oppo中的API是什么?