带通知的后台服务

问题描述 投票:0回答:2

我正在开发一个Android应用程序,它调用API来获取数据。现在我想在后台执行此任务,并且每次在列表视图中更改数据时,都应该生成通知。

我怎样才能实现这个目标?

如何在后台调用我的 API 以及如何生成通知。 我是服务和广播接收器的新手,所以请帮助我

我这样调用服务:

startService(new Intent(this, MyService.class).putExtra("Background",true));

我创建此代码是为了测试目的。检查即使应用程序关闭,是否可以在后台调用通知。

我的服务等级

public class MyService extends Service {

private Boolean isShowingNotification = true ;
NotificationManager notificationManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);


    if (intent.hasExtra("Background")) {
        if (isShowingNotification) {
            StopImportantJob();
            stopSelf();
        } else
            DoImportantJob();
    } else {
        DisplayNotification("Now showing the demo");
    }

    return START_NOT_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Toast.makeText(this, "On Create Called", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    notificationManager.cancelAll();
}

public void DisplayNotification(String message){

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    Notification notification = new Notification.Builder(this)
            .setContentTitle(message)
            .setContentText("Touch to off Service")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setOngoing(false)
            .build();

    notificationManager.notify(0,notification);

}

public void DoImportantJob(){

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    Notification notification = new Notification.Builder(this)
            .setContentTitle("New mail from " + "[email protected]")
            .setContentText("Subject")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setOngoing(false)
            .build();

    startForeground(1992, notification);
    isShowingNotification =true;
}

public void StopImportantJob(){

    stopForeground(true);
    isShowingNotification = false;
    if(false){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ){
            stopForeground(STOP_FOREGROUND_DETACH);
            stopForeground(STOP_FOREGROUND_REMOVE);

        }

    }
}

}

下面列出了我认为我应该做的,如果我错了,请纠正我。

  1. 在Application的MainActivity的onCreate()中启动Service。
  2. 在服务调用中,我将创建一个执行 API 调用的方法。
  3. 在notifyDataSetChanged()上;将调用通知方法。
  4. 现在问题是:在 Service 类中,API 方法将在 onCreate() 或 onStartCommand() 中调用。
java android push-notification background-service
2个回答
0
投票

现在我想在后台执行此任务,并且每次数据 在列表视图中更改后,应生成通知。怎么能 我实现了这个?我怎样才能让我的 API 在后台调用以及怎样才能 我生成通知。

首先决定,当您的应用程序处于

Foreground
Background
时是否需要此操作。现在,
if Foreground
,您可能不想使用
Service
类,而是使用
AsyncTask
来进行 Web 服务调用并生成通知并在任务完成后更新列表视图。
If Background
,您可以创建
IntentService
并在那里进行API操作。但是,在后台模式下,您的应用程序不需要收到通知,因为您的应用程序对客户端不可见。


0
投票

据我了解,Wolt 配送应用程序在后台始终有活动的通知类别,您可以将其停用。 Notifications setup... Wolt app

© www.soinside.com 2019 - 2024. All rights reserved.