你实施了错误的概念..,这就产生了问题..,去解决它。。你又犯了错误。。请卸载所有。。。
-
在android O中,根本不允许后台执行。。即使是一行代码也不能保证执行。。!!
-
解决方案;
每次应用程序第一次打开时都可以启动相同的服务。。。获取所有运行时权限。作为:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, YourLongRunningService.class));
}
else
{
context.startService(new Intent(context, YourLongRunningService.class));
}
OnStartCommand
开始分析和检查设备和应用程序状态的长时间运行任务。
问题;
-
OnStartCommand并不意味着要对长时间运行的过程/代码语句进行编码。。
-
解决方案;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("RUNNER : ", "\nPERFORMING....");
return START_STICKY;
}
一直到开发人员通过代码STOPSELF自行停止为止
甚至从
background
或来自
recents
;由android操作系统自动重新创建
那么我应该在哪里写代码??
开始长时间运行的任务
分析
和
应用程序状态
I can not resolve your symbols
:
-
分析
-
检查设备
-
应用程序状态
但我知道你的广播肯定。。你需要安装广播接收器。。。收到它。。。!!
我将实现广播接收器。。。太简单了。。。
问题;
在上面创建的服务中实现运行时接收器
YourLongRunningService
在里面
onCreate
比如:
IntentFilter myFilter = new IntentFilter();
myFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(mCallBroadcastReceiver, CallFilter);
是什么
mCallBroadcastReceiver
这些是接收器的运行时注册。。。需要从中注销
onDestroy
比如:
@Override
public void onDestroy()
{
this.unregisterReceiver(mCallBroadcastReceiver);
super.onDestroy();
}
START_STICKY
即使它被销毁;也会自动启动并注册
在里面
再一次。。
在这种情况下
mcall广播接收机
receiver是在类区域中定义的,在类区域中我们声明变量和常量:
public class YourLongRunningService extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";
private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// All your receiver code goes here...
}
};
// your constructor
// your onStartcommand
// your ondestroy
}
在O和以上,如果没有正确的操作,就不能运行前台服务
Ongoing
任务通知。。。它会在你的房间里
它将调用Startforeground并通知将此服务作为前台服务启动
安卓O。。。?
它也在Android下工作。。。只要用普通的startservice调用它在一开始就给出的代码。。。!!
从接收器接收您想要的广播并启动
intentservice
或者
job
或者警报课或者你想要的任何东西。。。
context
并定义一个名为
LongRunningCode
可能是一千万行代码。。。。
在接收器中接收广播时,我将通过传递
只需呼叫
方法
object