gpt4 book ai didi

android - 如何让服务保持活力?

转载 作者:太空狗 更新时间:2023-10-29 15:37:51 26 4
gpt4 key购买 nike

Whatsapp 服务如何在华为手机中保持后台运行?

我删除了 protected 应用程序的 whatsapp 但 Whatsapp 服务没有在屏幕上关闭休息时间。

我正在编写每次都需要运行的关键应用程序,但我的服务在屏幕关闭时终止。

我想写类似Whatsapp或AirDroid服务的服务谁能解释一下?

我的意思是在华为手机中如何写专门不熄屏关闭的服务

这是我的服务代码

AppLifeService

public class AppLifeService extends Service {
@Override
public IBinder onBind(Intent intent) {

return null;
}



@Override
public void onCreate() {
super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);


startForeground(5, AppLifeReciever.createNotification(this));


return START_STICKY;
}


@Override
public void onDestroy() {


//startService(new Intent(this, AppLifeService.class)); Updated : not need


super.onDestroy();

}
}

最佳答案

您需要创建一个 Service 以在 BroadcastService 关闭时自动“重新打开”。

例如:

广播服务

public class MyBroadcastService extends BroadcastReceiver
{

@Override
public void onReceive(final Context context, Intent intent)
{
//do something
}
}

服务自动“重启”

public class MyService extends Service
{

@Override
public void onCreate()
{
// Handler will get associated with the current thread,
// which is the main thread.
super.onCreate();
ctx = this;

}

@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub

return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "onStartCommand");
//Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show();

return START_STICKY;
}

//launch when its closed
@Override
public void onDestroy()
{
super.onDestroy();
sendBroadcast(new Intent("YouWillNeverKillMe"));
Toast.makeText(this, "YouWillNeverKillMe TOAST!!", Toast.LENGTH_LONG).show();
}
}

在您的 AndroidManifest.XML 上声明

<receiver android:name=".BroadcastServicesBackground.MyBroadcastService">
<intent-filter>
<!--That name (YouWillNeverKillMe) you wrote on Myservice-->
<action android:name="YouWillNeverKillMe"/>

<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<!--To launch on device boot-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<service android:name=".Services.MyService"/>

关于android - 如何让服务保持活力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474290/

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