【问题标题】:How to keep a Service alive?如何让服务保持活力?
【发布时间】:2016-07-20 06:55:33
【问题描述】:

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();

}
}

【问题讨论】:

  • 你检查过不同版本的安卓操作系统吗?在哪个操作系统版本服务停止?
  • 如果您的服务确实是前台(如您调用 startForeground),那么它不能被杀死,不可关闭的通知就是证明。你什么时候调用 stopForeground?

标签: android service whatsapp huawei-mobile-services


【解决方案1】:

使用START_STICKY 重新运行onStartCommand() 的服务将自动重新启动,您无需在onDestroy() 重新启动它

    @Override
    public void onDestroy() {

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

    }

【讨论】:

  • 谢谢,更新后。但我特别关注华为手机,该应用未添加到“受保护的应用”列表中
  • @hamidjahandideh 抱歉,我对华为手机一无所知。
  • 新用户进入此线程的信息,不保证在每种情况下都会调用 onDestroy。
【解决方案2】:

您需要创建一个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"/>

【讨论】:

  • 我在华为手机上测试过,service close 没有调用onDestroy
【解决方案3】:

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

我发现最好的方法是

startForeground(int, Notification)

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

您需要提供Notification 来显示在“正在进行”部分的通知栏中的方法。 这样,应用程序将在后台保持活动状态而不会受到任何干扰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多