gpt4 book ai didi

要求Android服务每十分钟持续运行一次

转载 作者:行者123 更新时间:2023-11-29 01:21:54 25 4
gpt4 key购买 nike

我希望即使应用程序关闭(终止)或即使用户没有启动应用程序,服务也能运行。我想在安装应用程序后启动该服务,从这一点开始,该服务需要每十分钟持续运行一次。

虽然我找到了一些解决办法比如

这些对我不起作用,因为我是 Android 编程的新手。如果有任何示例工作源代码,请向我提供如何开发它的信息。

这是我的服务类..

public class MyService extends Service {

MyReceiver receiver = new MyReceiver();

public void onCreate()
{
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

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

@Override
public void onStart(Intent intent, int startId)
{
receiver.SetAlarm(this);
}

}

这是我的接收器类..

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
try {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();

// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

wl.release();
}catch (Exception e) {
e.printStackTrace();
}
}

public void SetAlarm(Context context)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, UploadLocReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
}

}

我在 AndroidManifest.xml 文件中添加了服务和接收器。

<uses-permission android:name="android.permission.WAKE_LOCK" />

<service android:name=".MyService"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>

<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

我在主 Activity 中添加了以下代码。

Intent service = new Intent(getApplicationContext(), MyService.class);
this.startService(service);

最佳答案

Android 操作系统会在您安装应用程序时通知您。

1.ma​​nifest文件中添加receiver,它会通知你用户已经安装了应用:

<receiver android:name="com.mypackagename.Installreceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

2.Installreceiver 广播接收器开始每 10 分钟运行一次的服务。

public class Installreceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context.getApplicationContext(), YourService.class));
}
}

你的服务.java:

public class YourService extends Service {

private static String TAG = "MyService";
private Handler handler;
private Runnable runnable;
private final int runTime = 10000;

@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");

handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {

handler.postDelayed(runnable, runTime);
}
};
handler.post(runnable);
}

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

@Override
public void onDestroy() {
super.onDestroy();
}

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

更新 1:

3.如果操作系统终止了您的服务,那么您需要通过执行另一个接收器重新开始。

在服务中添加以下两个方法:

@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
sendBroadcast(new Intent("IWillStartAuto"));
}

@Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("IWillStartAuto"));
}

4.manifest中添加receiver

<receiver android:name=".RestartServiceReceiver" >
<intent-filter>
<action android:name="IWillStartAuto" >
</action>
</intent-filter>
</receiver>

5. 添加接收者:

public class RestartServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context.getApplicationContext(), YourService.class));
}
}

更新 2:

6.在 list 中添加权限。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

7.在 list 中添加接收者。

<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

7. BootCompletedReceiver,java:

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
context.startService(new Intent(context.getApplicationContext(), YourService.class));
}
}

希望对您有所帮助。

关于要求Android服务每十分钟持续运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327359/

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