如何使服务保持活力?

ham*_*deh 2 service android whatsapp huawei

Whatsapp服务如何继续在华为手机中运行?

我删除了受保护的应用程序的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();

}
}
Run Code Online (Sandbox Code Playgroud)

Soh*_*hid 5

START_STICKY在retrun中的服务onStartCommand()将自动重新开始,您不需要再次启动它onDestroy()

    @Override
    public void onDestroy() {

      //  startService(new Intent(this, AppLifeService.class));
        super.onDestroy();

    }
Run Code Online (Sandbox Code Playgroud)


Asp*_*cas 5

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

例如:

广播服务

public class MyBroadcastService extends BroadcastReceiver
{

 @Override
    public void onReceive(final Context context, Intent intent)
    {
     //do something
    }
}
Run Code Online (Sandbox Code Playgroud)

服务自动“重新打开”

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

声明您的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"/>
Run Code Online (Sandbox Code Playgroud)

  • 我在华为手机中进行了测试,服务关闭而无需调用 onDestroy (2认同)

Xar*_*mer 5

@Sohail Zahid答案告诉您一种在服务停止时一次又一次repeatedly启动服务的方法。但为了保持服务的活力,就像在背景中播放歌曲一样。

我发现的最好的方法是

startForeground(int, Notification)
Run Code Online (Sandbox Code Playgroud)

其中 int 值对于每个通知必须是唯一的

您需要Notification向“正在进行”部分的通知栏中显示的方法提供 。通过这种方式,应用程序将在后台保持活动状态而不会出现任何中断。