gpt4 book ai didi

android - 永远在后台运行服务 - Android

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:31 25 4
gpt4 key购买 nike

我在我的 android 应用程序中创建了一个 Service,它通过 BroadcastReceiverBOOT_COMPLETE 上自动启动。这工作得很好。但是此服务仅执行一次我在 onCreate() 方法中定义的任务。另一方面,我想在后台永远运行 Service 。实际上,在 onCreate() 方法中,我正在从我的数据库中读取数据,并在需要时生成通知。通知可以随时生成,因此我想永远运行我的服务。我是 Android 的新手,我看过很多示例和教程,但它们没有帮助。请回答我如何才能永远运行我的 Service

这是服务代码:

package com.example.abc.project1;

import android.app.*;
import android.content.Intent;
import android.os.IBinder;
import android.util.*;
import org.json.*;
import java.io.*;
import java.net.*;
import java.util.*;


public class HelloService extends Service {

private static final String TAG = "HelloService";
public static boolean isRunning = false;

@Override
public IBinder onBind(Intent arg0) {
return null;
}


@Override
public void onCreate() {
isRunning = true;
new Thread(new Runnable() {
@Override
public void run() {
/*Here I want to do my task forever (reading from database & generate notifications)*/
}
}).start();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}

@Override
public void onDestroy() {
isRunning = false;
}

}

这是BroadcastReceiver的代码

package com.example.abc.project1;

import android.content.*;
import android.util.Log;

public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}

这是 AndroidManifest.xml 的一部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.project1">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gef.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_CATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<service android:name=".HelloService"></service>

<receiver android:name="com.example.abc.project1.MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>

</application>

</manifest>

最佳答案

根据 Service documentation

The service will at this point continue running until Context.stopService() or stopSelf() is called.

因此您的服务将永远运行,直到系统不会因为缺乏资源而决定终止它。

关于android - 永远在后台运行服务 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37090456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com