关闭应用程序后,不会调用广播接收器

时间:2016-12-01 14:13:17

标签: broadcastreceiver

我正在使用Android 4.4.4版Hauwei设备运行。

当我打开我的应用程序时,它的广播接收器工作正常,但当我通过关闭所有最近的应用程序关闭应用程序时,广播接收器就停止触发。我搜索了很多,但我找不到解决方案。

权限:

  <uses-permission android:name="android.permission.READ_SMS"/>
  <uses-permission android:name="android.permission.SEND_SMS"/>
  <uses-permission android:name="android.permission.RECEIVE_SMS"/>

我已将接收器放在清单文件中:

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name=".MySmsReceiver"
        >
        <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>


    <activity android:name=".MainActivity">
        <intent-filter>

            <action android:name="COM.EXAMPLE.SIVA.MAINACTIVITY" />


            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    </application>

这是我的广播接收器:

   public class MySmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "sms received", Toast.LENGTH_SHORT).show();
}
  }

清除最近的应用程序后,我在默认设置应用程序中选中了我的应用程序,我的应用程序强制停止按钮被禁用..

2 个答案:

答案 0 :(得分:0)

您必须创建服务:

的AndroidManifest.xml

<service android:name=".SomeService"
        android:process="com.example.SomeService"
        android:enabled="true"
        />

SomeService.java

public class SomeService extends Service{

   public static final String START_SERVICE = "start";
   public static final String STOP_SERVICE = "stop";

   private SomeReceiver someReceiver;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        switch (action) {
            case START_SERVICE:
                if (someReceiver == null) {
                    createReceiver();
                }
                registerReceiver(intent);
                startForeground((int) Thread.currentThread().getId(), new NotificationCompat.Builder(getBaseContext()).build());
                break;
            case STOP_SERVICE:
                unregisterReceiver();
                stopSelf();
        }

        return START_STICKY;
    }

    private void createReceiver() {
        someReceiver = new SomeReceiver();
        filter = new IntentFilter();
        filter.addAction(ACTION_TIME_TICK);
    }

    private void registerReceiver(Intent intent){
        unregisterReceiver();

        registerReceiver(someReceiver, filter);
    }

    private void registerReceiver(){
        unregisterReceiver();

        registerReceiver(someReceiver, filter);
    }

    private void unregisterReceiver(){
        try{
            unregisterReceiver(someReceiver);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

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

这个接收器正在听时间滴答声。 (ACTION_TIME_TICK - 每分钟发生一次)

public class SomeReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
       Log.i("SomeReceiver", "Tick");   
    }

}

ActivityMain.java

...
startService(this, SomeService.class);
...

所以现在它不需要在AndroidManifest.xml中注册接收器。

希望,这有帮助

答案 1 :(得分:0)

manifest-declared receivers上的Android文档描述了阻止隐式广播的Android行为更改。它们还链接到另一种方法。

如果您的应用程序的目标是API级别26或更高级别,则您不能使用清单为隐式广播(不专门针对您的应用程序的广播)声明接收方,除了一些不受该限制的隐式广播。在大多数情况下,您可以改用scheduled jobs