如果我也杀死应用实例,我想在后台运行我的应用。但是在我杀死我的应用程序后,该服务也停止了工作。这是我的代码,请任何人帮助我解决问题。
我跟随着此链接在后台运行,但是如果删除该实例,它将无法正常工作。如果该实例也被删除,请问有人可以告诉我如何运行后台服务吗?
这是我的MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ALARM_REQUEST_CODE, alarmIntent, 0);
mSensorService = new SensorService(getCtx());
mServiceIntent = new Intent(getCtx(), mSensorService.getClass());
if (!isMyServiceRunning(mSensorService.getClass())) {
startService(mServiceIntent);
}
}
这是我的服务水平
public class SensorService extends Service{
public int counter=0;
public SensorService(Context applicationContext) {
super();
Log.i("HERE", "here I am!");
}
public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
Log.i("in timer", "in timer ++++ "+ (counter++));
}
};
}
/**
* not needed
*/
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 0 :(得分:2)
故事很长。我经历了它。仍然实施。现在,我的服务在每个boot_complete事件上运行,并一直保持运行(带有通知)。
大号Google android开发人员文档很差,也没有适当的示例示例。这是理论上的,只是理论上的。继续阅读,如果有兴趣
https://developer.android.com/about/versions/oreo/background
简介1:
在传统接收器中,您只能接收BOOT_COMPLETE
且只有很少的广播。通过始终运行的服务中的代码注册它们,从而使您需要在服务中实现运行时的所有广播接收器都得到休息。
简介2:
同样,您不能始终在8.0(Oreo)或更高版本中运行进程...
要实现始终运行的过程,请执行以下操作:创建一个Intentservice
,并通知其类型为ongoing
,使OnStartCommand
START_STICKY
并在OnCreate
中向代码注册接收者>
如何实施: 我已经实现了,可以从这里参考: Oreo: Broadcast receiver Not working
现在您的问题:如果应用杀死了,我想在后台运行我的应用 应用实例。
借助我自己的上述实现链接,您可以实现
*条款和条件
您的设备必须已发布正确的android操作系统,并且已按原样刻录。
是的,我正在使用android:
No... You are Using Funtouch OS : VIVO ( By modifying Android)
COLOR OS市场上有很多设备:OPPO(通过修改Android) .... ....
to allow only selective applications run in background
像WhatsApp
,Facebook
,Google
Twitter
Instagram
现在您将询问开发人员问题:如果这些应用程序在后台运行,那么我也可以使我的应用程序在后台运行。...
否...它们是基于OS的修改,用于检查服务是否来自允许的供应商,然后只有它可以在后台存活。如果他们不允许这些供应商,那么没人会选择没有运行这些著名社交应用程序的手机。
Hushhhhhhhh .......
答案 1 :(得分:0)
您需要创建ForegroundService才能在您的应用被终止时继续处理,如下所示:
public class SensorService extends Service{
private PowerManager.WakeLock wakeLock;
@Override
public void onCreate() {
super.onCreate();
//wake lock is need to keep timer alive when device goes to sleep mode
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK_TAG");
createNotificationChannel(this);
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL").setSmallIcon
(<icon>).setContentTitle("Title")
.setContentText("Content").build();
startForeground(1001, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
if (wakeLock.isHeld()) {
wakeLock.release();
}
}
public void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel name";
String description = "Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("NOTIFICATION_CHANNEL", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
要启动服务:
Intent i = new Intent(context, SensorService.class);
ContextCompat.startForegroundService(context, i)
注意:
stopSelf()
。答案 2 :(得分:0)
新概念画中画(图片模式下的图片) 并通过设置渠道和优先级来进行类别服务控制。您仅需更改代码即可让oreo创建通知和服务
在此处仔细阅读有关Google开发人员的文档 https://developer.android.com/guide/topics/ui/notifiers/notifications Java和Kotlin代码都可以在此处用于在oreo中创建通知
https://developer.android.com/training/notify-user/build-notification
我是在与您搜索并共享后寻找解决方案的努力。
这是一些示例代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
用于创建频道的代码如下:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
通过单击以上链接,您可以查看有关推送通知和发送消息的详细信息。