如果从最近的应用程序中删除了应用程序,则不会调用接收方

时间:2018-08-04 16:29:10

标签: java android broadcastreceiver

我正在开发可在活动通话结束时发送短信的应用程序。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxxx.xxxx.xxxx">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <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>

    <service
        android:name=".MonitorService"
        android:enabled="true"
        android:exported="true" />
    <service
        android:name=".SendText"
        android:exported="false" />

    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

</manifest>

如果MainActivity中的棉花糖也超过了棉花糖,我已经获得了所有运行时权限。

我的接收器:

public class BootReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
    Log.d("BootTest : ", "\nOnBootReceiver - Received a broadcast!");
    Toast.makeText(context, "InstaPromo Is Ready !!", Toast.LENGTH_LONG).show();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        context.startForegroundService(new Intent(context, MonitorService.class));
    }
    else
    {
        context.startService(new Intent(context, MonitorService.class));
    }
}
}

实现的接收器运行时为:

public class MonitorService extends Service
{
// Notification variables

private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
        {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                Log.d("RECEIVER X: ", "INCOMING CALL...");
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                Log.d("RECEIVER X: ", "CALL ENDS HERE...");
                Intent Dispatcher = new Intent(context, SendText.class);
                startService(Dispatcher);
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                Log.d("RECEIVER X: ", "ACTIVE CALL GOING ON...");
            }
        }
    }
};

public MonitorService() { }

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

    // created notification here
    // also registered broadcast receiver here

    mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
    startForeground(17, mBuilder.build());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.d("WatchMan : ", "\nmCallBroadcastReceiver Listening....");

    //return super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

@Override
public void onDestroy()
{
    this.unregisterReceiver(mCallBroadcastReceiver);
    Log.d("WatchMan : ", "\nDestroyed....");
    Log.d("WatchMan : ", "\nWill be created again....");
}

@Override
public IBinder onBind(Intent intent)
{
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
}
  • Broadcastreceiver在服务级别上工作正常;即使从最近的应用程序列表中删除了该应用程序,但是如果将其从最近的应用程序列表中删除,则在下一个boot_complete上,它也不会再调用接收方。如果未将App从最近的版本中删除...则在每次boot_complete /快速启动时,都会触发它,并且广播接收器可以正常工作。我尝试了android:exclude_from_recents ...,但这不是实现它的方法。

有人可以帮助我解决这种情况吗?谢谢

更新2

我在github上研究了Firebase JobDispatcher文档,并按如下方式使用它:

  1. gradle项目文件中实现的依赖性
  2. 然后同步成功
  3. 创建了JobService
  4. 创建工作并安排工作。
  5. 它也在boot_completed上触发。

public class MyJobService extends JobService
{
@Override
public boolean onStartJob(JobParameters job)
{
    // Do some work here

    Log.d("MY_JOB :", "STARTED HERE...  \n");

    return false;

    // Answers the question: "Is there still work going on?"
}

@Override
public boolean onStopJob(JobParameters job)
{
    Log.d("MY_JOB :", "STOPPED HERE...  \n");

    return true;

    // Answers the question: "Should this job be retried?"
}
}

        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(MainActivity.this));

    Bundle myExtrasBundle = new Bundle();
    myExtrasBundle.putString("some_key", "some_value");

    Job myJob = dispatcher.newJobBuilder()
            // the JobService that will be called
            .setService(MyJobService.class)
            // uniquely identifies the job
            .setTag("my-unique-tag")
            // one-off job
            .setRecurring(false)
            // don't persist past a device reboot
            .setLifetime(Lifetime.FOREVER)
            // start between 0 and 60 seconds from now
            .setTrigger(Trigger.executionWindow(10, 10+2))
            // don't overwrite an existing job with the same tag
            .setReplaceCurrent(false)
            // retry with exponential backoff
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            // constraints that need to be satisfied for the job to run
            .setConstraints(
                    // only run when device is idle
                    Constraint.DEVICE_IDLE
            )
            .setExtras(myExtrasBundle)
            .build();

    dispatcher.mustSchedule(myJob);

我应如何安排它仅在重新启动/启动完成时运行一次?

  1. 我只想在停止后再次出现。.:setRecurring(false),可以将它设置为false或true吗?
  2. setLifetime(Lifetime.UNTIL_NEXT_BOOT)永久设置为可以
  3. setTrigger(Trigger.executionWindow(0,60))未获取
  4. setReplaceCurrent(false)是的。
  5. 以指数补偿重试这是什么?

如何设置这些标签以仅在BOOT / REBOOT上启动,仅尝试一次,如果启动失败,请尝试再次启动。.

2 个答案:

答案 0 :(得分:1)

每次启动后,请使用FirebaseJobDispatcher重新启动服务。有关JobDispatcher的更多信息,请参见this

答案 1 :(得分:1)

  • 如果用户首先安装了该应用强制停止了该应用,则BroadcastReceviers将不会运行,除非用户在上手动运行该应用至少一次

  • docanswer

      
        
    • 请注意,应用程序的停止状态 Activity's停止状态不同。系统分别管理这两个两个停止状态

    •   
    • 应用程序在首次安装但尚未启动时以及在手动安装时处于停止状态   已被用户停止(在“管理应用程序”中)。 (它们表示力   停止应用

    •   
  • Htc设备从此answer添加com.htc.intent.action.QUICKBOOT_POWERON >>。

    <receiver android:enabled="true" android:name=".receivers.BootUpReceiver">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
         </intent-filter>
    </receiver>
    

Starting from Android O

  • 您不能从后台应用程序启动service,除非有例外:java.lang.IllegalStateException: Not allowed to start service Intent (my_service):应用程序处于后台状态如果您仍然需要在设备启动时启动服务,则可以使用new JobIntentService.

将您的BroadcastReceiver和JobIntentService添加到清单中

<receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

<service android:name=".MyService"
         android:permission="android.permission.BIND_JOB_SERVICE"/>

让您的JobIntentService入队:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            MyService.enqueueWork(context, new Intent());
        }
    }

}

定义您的JobIntentService:

public class MyJobIntentService extends JobIntentService {

    public static final int JOB_ID = 0x01;

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, MyJobIntentService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        // your code
    }

/**
 * This will be called if the JobScheduler has decided to stop this job.  The job for
 * this service does not have any constraints specified, so this will only generally happen
 * if the service exceeds the job's maximum execution time.
 *
 * @return True to indicate to the JobManager whether you'd like to reschedule this work,
 * false to drop this and all following work. Regardless of the value returned, your service
 * must stop executing or the system will ultimately kill it.  The default implementation
 * returns true, and that is most likely what you want to return as well (so no work gets
 * lost).
 */
public boolean onStopCurrentWork() {
    return true;
}

}