如何替换 Android 中的 sendStickyBroadcast?
How to replace sendStickyBroadcast in Android?
如您所知,
sendStickyBroadcast 方法现在是 deprecated。如何更换?
我当然可以使用sendBroadcast,但那样就不会粘了。
这是 Google 对粘性广播为何被弃用的解释。
不应使用粘性广播。它们不提供安全性(任何人都可以访问它们)、没有保护(任何人都可以修改它们)以及许多其他问题。推荐的模式是使用非粘性广播来报告某些内容已更改,并使用另一种机制让应用程序在需要时检索当前值。
希望对您有所帮助。
您可以使用事件总线,以下是一些最常用的库。
- https://github.com/greenrobot/EventBus
- http://square.github.io/otto/
- https://blog.kaush.co/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/(如何使用 Rx 作为事件总线)
另一种方法是创建一个 class 来侦听广播,然后存储它检索到的最后一个状态。在我看来,这种方法并不理想。
也许可以用 JobScheduler 到
安排定期作业,
它将发送广播。
“keep alive”服务,它将发送 periodoc 广播。
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import static my.UtilsLocation.PACKAGE_NAME;
/**
* JobService to be scheduled by the JobScheduler.
* start another service
*/
public class KeepAliveBroadcastJobService extends JobService {
public static final String INTENT_ACTION_KEEP_ALIVE = PACKAGE_NAME + ".action.KEEPALIVE";
@Override
public boolean onStartJob(JobParameters params) {
// send recurring broadcast
final Intent intent = new Intent(getApplicationContext());
intent.setAction(INTENT_ACTION_KEEP_ALIVE);
sendBroadcast(intent);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
一个实用程序,用于定期安排保持活动作业。
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import java.util.concurrent.atomic.AtomicBoolean;
public class UtilsKeepAlive {
private static final String TAG = UtilsKeepAlive.class.toString();
private static AtomicBoolean isKeepAliveOn = new AtomicBoolean(false);
private static final int INTERVAL_MILLIS = 600000; // 10 min
private static final int FLEX_MILLIS = 60000; // 1 min
public static void enableKeepAlive(Context context) {
// if already on
if (isKeepAliveOn.get()) return;
Log.i(TAG, "Keep alive job scheduled");
ComponentName serviceComponent = new ComponentName(context, KeepAliveBroadcastJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); //Require any network
builder.setRequiresCharging(false);
builder.setPeriodic(INTERVAL_MILLIS, FLEX_MILLIS);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
jobScheduler.schedule(builder.build());
//we have scheduled the keep alive
isKeepAliveOn.set(true);
}
}
周期性的“保持活动”工作 - 可以是例如安排在 BOOT_COMPLETED.
的广播中
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BroadCastReceiver got the location.");
final String action = intent.getAction();
switch (action) {
case INTENT_ACTION_BOOT_COMPLETED:
Log.i(TAG, "Received a BootCompleted");
UtilsKeepAlive.enableKeepAlive(context);
break;
我使用本教程解释了 JobScheduler:
https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
如您所知, sendStickyBroadcast 方法现在是 deprecated。如何更换?
我当然可以使用sendBroadcast,但那样就不会粘了。
这是 Google 对粘性广播为何被弃用的解释。
不应使用粘性广播。它们不提供安全性(任何人都可以访问它们)、没有保护(任何人都可以修改它们)以及许多其他问题。推荐的模式是使用非粘性广播来报告某些内容已更改,并使用另一种机制让应用程序在需要时检索当前值。
希望对您有所帮助。
您可以使用事件总线,以下是一些最常用的库。 - https://github.com/greenrobot/EventBus - http://square.github.io/otto/ - https://blog.kaush.co/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/(如何使用 Rx 作为事件总线)
另一种方法是创建一个 class 来侦听广播,然后存储它检索到的最后一个状态。在我看来,这种方法并不理想。
也许可以用 JobScheduler 到
安排定期作业,
它将发送广播。
“keep alive”服务,它将发送 periodoc 广播。
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import static my.UtilsLocation.PACKAGE_NAME;
/**
* JobService to be scheduled by the JobScheduler.
* start another service
*/
public class KeepAliveBroadcastJobService extends JobService {
public static final String INTENT_ACTION_KEEP_ALIVE = PACKAGE_NAME + ".action.KEEPALIVE";
@Override
public boolean onStartJob(JobParameters params) {
// send recurring broadcast
final Intent intent = new Intent(getApplicationContext());
intent.setAction(INTENT_ACTION_KEEP_ALIVE);
sendBroadcast(intent);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
一个实用程序,用于定期安排保持活动作业。
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import java.util.concurrent.atomic.AtomicBoolean;
public class UtilsKeepAlive {
private static final String TAG = UtilsKeepAlive.class.toString();
private static AtomicBoolean isKeepAliveOn = new AtomicBoolean(false);
private static final int INTERVAL_MILLIS = 600000; // 10 min
private static final int FLEX_MILLIS = 60000; // 1 min
public static void enableKeepAlive(Context context) {
// if already on
if (isKeepAliveOn.get()) return;
Log.i(TAG, "Keep alive job scheduled");
ComponentName serviceComponent = new ComponentName(context, KeepAliveBroadcastJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); //Require any network
builder.setRequiresCharging(false);
builder.setPeriodic(INTERVAL_MILLIS, FLEX_MILLIS);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
jobScheduler.schedule(builder.build());
//we have scheduled the keep alive
isKeepAliveOn.set(true);
}
}
周期性的“保持活动”工作 - 可以是例如安排在 BOOT_COMPLETED.
的广播中@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BroadCastReceiver got the location.");
final String action = intent.getAction();
switch (action) {
case INTENT_ACTION_BOOT_COMPLETED:
Log.i(TAG, "Received a BootCompleted");
UtilsKeepAlive.enableKeepAlive(context);
break;
我使用本教程解释了 JobScheduler: https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html