相关疑难解决方法(0)

尝试在Android上启动服务

当设备在Android上启动时,我一直在尝试启动服务,但我无法让它工作.我在网上看了很多链接,但没有一个代码可以工作.我忘记了什么吗?

AndroidManifest.xml中

<receiver
    android:name=".StartServiceAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action._BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.test.RunService"
    android:enabled="true" />
Run Code Online (Sandbox Code Playgroud)

广播接收器

public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceLauncher = new Intent(context, RunService.class);
        context.startService(serviceLauncher);
        Log.v("TEST", "Service loaded at start");
    }
}
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-service

327
推荐指数
10
解决办法
19万
查看次数

Android - 为服务实现startForeground?

所以我不确定在何处/如何实现此方法以使我的服务在前台运行.目前我在另一项活动中通过以下方式开始我的服务:

Intent i = new Intent(context, myService.class); 
context.startService(i);
Run Code Online (Sandbox Code Playgroud)

然后在myServices的onCreate()中我尝试startForeground()......?

Notification notification = new Notification();
startForeground(1, notification);
Run Code Online (Sandbox Code Playgroud)

所以是的,我有点失落,不确定如何实现这一点.

service android android-service

117
推荐指数
7
解决办法
16万
查看次数

当电话进入空闲状态时,后台服务停止

我有一个粘性的后台服务,必须一直运行.基本上它跟踪时间,它是一个自定义时钟计时器.但是一旦设备进入空闲状态(当电话屏幕关闭时),定时器(后台服务)也会暂停.

即使屏幕关闭,我该怎么做才能使它始终保持运行?

    public class TimerService extends Service {
    private Context context;
    @Override
        public IBinder onBind(Intent intent) {
            Logger.LogI(TAG, "Service binding");
            return null;
        }

      @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
       return Service.START_STICKY;
        }
    }
Run Code Online (Sandbox Code Playgroud)

performance service android

2
推荐指数
1
解决办法
5560
查看次数