没有一个...Many problems
在你的代码中......你可能会得到“0错误”,因为它在语法上是正确的,但它是androidicaly
错了,你的basics
很穷,读书android documentation
and implementation
很穷。 Android从来不会运行非常糟糕的东西......
问题:1
您是否知道按照惯例您应该获得一项服务override
onCreate
, onStartCommand
, onBind
, onDestroy
方法....?
我在那里没有看到 onDestroy...!!
问题:2
你知道如何通知...?你的onStartCommand
实施再次毫无意义。
保持为空只需返回 START_STICKY
问题:3
您希望如何在后台执行限制下运行它......?首先通过在中进行通知来通知androidoncreate
仅且与startforeground
如果需要的话...
我在那里看不到它......你试图这样做onstartcommand
再说一遍,它非常糟糕......
好吧...看看下面的工作代码:
public class RunnerService extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";
public RunnerService() { }
@Override
public void onCreate()
{
super.onCreate();
Log.d("RUNNER : ", "OnCreate... \n");
Bitmap IconLg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground);
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("My App")
.setContentText("Always running...")
.setTicker("Always running...")
.setSmallIcon(R.drawable.ic_menu_slideshow)
.setLargeIcon(IconLg)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[] {1000})
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setAutoCancel(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(1, mBuilder.build());
}
else
{
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyManager.notify(1, mBuilder.build());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("RUNNER : ", "\nPERFORMING....");
return START_STICKY;
}
@Override
public void onDestroy()
{
Log.d("RUNNER : ", "\nDestroyed....");
Log.d("RUNNER : ", "\nWill be created again automaticcaly....");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent)
{
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("NOT_YET_IMPLEMENTED");
}
}
如何检查....???
从最近使用的列表中删除该应用程序,您应该在日志中看到“Performing
” 消息中logcat
...
在什么情况下它会停止...?
它永远不会停止(直到下次启动..!!)...是的,当用户强制停止应用程序时它会停止。而且很少有系统发现它的资源非常低......这是一种非常罕见的情况,因为 android 随着时间的推移已经有了很大的改进......
怎么开始呢....????
无论它来自哪里mainactivity
或来自receiver
或来自任何class
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, RunnerService.class));
}
else
{
context.startService(new Intent(context, RunnerService.class));
}
如何检查服务是否启动......?
根本不...即使您启动服务多少次...如果它已经在运行...那么它不会再次启动...如果没有运行那么...将开始它...!!