我希望每当用户打开应用程序时运行一项服务,然后只要每隔1分钟安装一次该应用程序便使其运行。该服务将通过调用API来检查是否有新订单,如果有则将显示通知。
我已经进行了一些研究,发现JobIntentService和Broadcast Reciever是解决问题所需要的,但我无法将它们组合在一起以使其正常工作。如果有人可以提供详细的解决方案,我将不胜感激。我还想知道是否有比我的问题更好的东西?下面是我设法编写的代码。
后台服务
public class BackgroundService extends JobIntentService {
private static final String TAG = BackgroundService.class.getName();
private static final int JOB_ID = 1;
private NotificationManager notificationManager;
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, BackgroundService.class, JOB_ID, intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, getClass().getSimpleName() + "#onCreate");
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Running in background!");
sendBroadcast(new Intent(getApplicationContext(), BackgroundService.class));
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d(TAG, getClass().getSimpleName() + "#onHandleWork!");
APIInterface service = RetrofitClientInstance.getRetrofitInstance().create(APIInterface.class);
Call<List<OrdersModel>> call = service.getAllOrders(Services.CONSUMER_KEY,Services.CONSUMER_SECRET);
call.enqueue(new Callback<List<OrdersModel>>() {
@Override
public void onResponse(@NonNull Call<List<OrdersModel>> call,@NonNull Response<List<OrdersModel>> response) {
if (response.body() != null) {
for (OrdersModel orders : response.body()){
if (orders.getStatus().equals("processing")){
sendNotification("#" + orders.getNumber());
}
}
}
}
@Override
public void onFailure(@NonNull Call<List<OrdersModel>> call,@NonNull Throwable t) {
Log.d(TAG, getClass().getSimpleName() + "Network Error!");
}
});
}
private void sendNotification(String title) {
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, GetOrdersActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentText("New Order")
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_MESSAGE);
notificationManager.notify(0, notificationBuilder.build());
Log.d("GoParty", "Notification sent ----- ");
}
}
BroadcastReciever
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, BackgroundService.class);
BackgroundService.enqueueWork(context, notificationIntent);
}
}
答案 0 :(得分:0)
那不是应该的工作方式。移动设备上的应用必须保留电池寿命。轮询效果不佳。您想要的是一个推送通知,服务器正在其中发挥作用。例如,您可以使用Firebase Cloud Messaging来实现。
看看:https://firebase.google.com/docs/cloud-messaging/android/client?authuser=0
服务器负责知道何时有新订单可用,并且可以在恰好适当的时机采取行动,向客户应用发送推送通知。