我想在后台运行服务,即使应用程序从堆栈中死亡。此功能在某些设备上正常工作。但在OPPO和vivo手机中,如果应用程序被杀,它将不会运行。有没有解决方案。如果没有,那么我如何打开允许屏幕。android中的后台服务没有运行oppo,vivo等设备
这里是我的代码:
Timer_Service.java
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.text.LoginFilter;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static com.gov.ojas1.WebView.TimerReceiver.CUSTOM_INTENT;
public class Timer_Service extends Service {
public static String str_receiver = "com.countdowntimerservice.receiver";
private Handler mHandler = new Handler();
Calendar calendar;
SimpleDateFormat simpleDateFormat;
String strDate;
Date date_current, date_diff;
SharedPreferences mpref;
SharedPreferences.Editor mEditor;
private Timer mTimer = null;
public static final long NOTIFY_INTERVAL = 1000;
Intent intent;
long int_timer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mEditor = mpref.edit();
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
intent = new Intent(str_receiver);
Random r = new Random();
int i1 = r.nextInt(10800000 - 7200000) + 7200000;
// int i1 = r.nextInt(300000 - 180000) + 180000;
// int_timer = (long) (int) i1;
int_timer =10000;
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
strDate = simpleDateFormat.format(calendar.getTime());
Log.e("strDate", strDate);
twoDatesBetweenTime();
}
});
}
}
public String twoDatesBetweenTime() {
try {
date_current = simpleDateFormat.parse(strDate);
} catch (Exception e) {
}
try {
date_diff = simpleDateFormat.parse(mpref.getString("data", ""));
} catch (Exception e) {
}
try {
long diff = date_current.getTime() - date_diff.getTime();
Log.e("Diff", diff + "");
Log.e("TImer", int_timer + "");
long long_hours = int_timer - diff;
long diffSeconds2 = long_hours/1000 % 60;
long diffMinutes2 = long_hours/(60 * 1000) % 60;
long diffHours2 = long_hours/(60 * 60 * 1000) % 24;
if (long_hours > 0) {
String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
Log.e("TIME", str_testing);
} else {
String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
mEditor.putString("data", strDate).commit();
fn_update();
Log.e("TIME", "Finish");
}
} catch (Exception e) {
mTimer.cancel();
mTimer.purge();
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
e.printStackTrace();
}
return "";
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Service finish", "Finish");
}
private void fn_update() {
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
i.putExtra("boolean", true);
sendBroadcast(i);
}
}
在此先感谢。
2017-05-17
Dev
+0
为主要在Android手机中发生的任何事件创建一个broadCast接收器。并在那里开始你的服务。 –
+0
或者使用Alarm Manager来代替。 –
+0
我有更新我的问题,并添加我现在使用的服务类。其实它在三星,联想等工作良好。但这项服务不会自动再次启动oppo,vivo和其他新的os设备 –