【发布时间】:2019-07-22 12:07:39
【问题描述】:
在我的应用程序中,它每 15 秒在后台加载一个 url(API)。我的应用程序不需要任何类型的 UI,所以我只需在我的服务类中加载 url。为了在 oreo+ 设备上正常工作,我使用了 startForgroundService()。但是在应用程序被杀死后,它在 Oreo 中不起作用。我的测试设备是OPPO Oreo 8.1.0。它在三星 7.0 上完美运行。
这是我的服务代码:
public class MyService extends Service {
Handler handler = new Handler();
Runnable runnable;
int delay = 15 * 1000;
String data;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
data = intent.getStringExtra("data");
loadURL(data);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("URL App status :")
.setContentText("The app is running in background")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
handler.postDelayed(runnable = new Runnable() {
public void run() {
Toast.makeText(MyService.this, "" + data, Toast.LENGTH_SHORT).show();
loadURL(data);
handler.postDelayed(runnable, delay);
}
}, delay);
return START_STICKY;
}
@Override
public void onDestroy() {
stopSelf();
}
public void loadURL(String data) {
try {
RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();
final String mURL = "http://localhost/att.php?emp_id=" + data + "&status=&submit=Insert";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
mURL, new JSONObject(), requestFuture, requestFuture);
MySingleton.getInstance(this).addToRequestQueue(request);
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
通知的App.class:
public class App extends Application {
public static final String CHANNEL_ID = "channel ID";
@Override
public void onCreate() {
super.onCreate();
createnotificationChannel();
}
private void createnotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "URL channel ID",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(serviceChannel);
}
}
}
还有启动前台服务的MainActivity:
public class MainActivity extends AppCompatActivity {
String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getIntent().getStringExtra("ID");
Intent intent = new Intent(MainActivity.this, MyService.class);
intent.putExtra("data", data);
ContextCompat.startForegroundService(this,intent);
}
我已经搜索了很多,但我无法找出我的代码有什么问题。只是需要帮助!
【问题讨论】:
-
请检查第一个服务是否在前台运行。
-
杀死应用时没有服务没有运行
-
成为前台并不意味着你不能被杀死,它只会降低你在优先级列表中的位置。您仍然不能指望在 Android 中永远运行的服务。而且我非常非常怀疑您是否需要在后台每 15 秒下载一个 url,这可能是您真正想做的任何事情的糟糕实现。
标签: android android-service android-volley foreground-service