未调用 Android 服务

Posted

技术标签:

【中文标题】未调用 Android 服务【英文标题】:Android service not called 【发布时间】:2017-10-15 10:38:23 【问题描述】:

这是我的服务:

public class KeepAliveService  extends Service 
Alarm alarm = new Alarm();
public void onCreate()

    Log.i("","KEEPALIVE onCreate");
    super.onCreate();


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

    alarm.setAlarm(this);
    Log.i("","KEEPALIVE onCreate start command");
    return START_STICKY;


@Override
public void onStart(Intent intent, int startId)

    alarm.setAlarm(this);
    Log.i("","KEEPALIVE onStart");


@Override
public IBinder onBind(Intent intent)

    return null;


我在我的清单中声明了这样的声明,在我之后:

   <?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<application
    android:icon="@drawable/ic_launcher"
    android:name="com.vidyo.vidyomod.VidyoModApplication"
    android:theme="@style/AppTheme"
    android:label="@string/app_name"
    android:allowBackup="true"
    android:supportsRtl="true"
    tools:replace="android:icon">
    <activity
        android:name="com.vidyo.vidyomod.activities.BaseActivity"
        android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="vidyocore" />
        </intent-filter>
    </activity>
    <service
        android:name="com.vidyo.vidyomod.KeepAliveService"
        android:enabled="true"
        android:process=":your_service" >
    </service>
    <receiver android:process=":remote" android:name="com.vidyo.vidyomod.utils.Alarm"></receiver>
    <receiver android:name="com.vidyo.vidyomod.utils.AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>
</application>

在我的 BaseActivity 的 onCreate 上,我这样做:

    Intent i= new Intent(BaseActivity.this, KeepAliveService.class);
    startService(i);

我调试过,我在 startService 处的断点确实停止了,但没有调用 OnCreate。为什么?

【问题讨论】:

【参考方案1】:

你必须把你的Service 和你的BroadcastReceiver 放在你的application 标签中:

<application>
<service
    android:name="com.vidyo.vidyomod.KeepAliveService"
    android:enabled="true"
    android:process=":your_service" >
</service>
<receiver android:name="com.vidyo.vidyomod.utils.AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
</application>

另外,您正在不同的进程中启动服务。

<service
    android:name="com.vidyo.vidyomod.KeepAliveService"
    android:enabled="true"
    android:process=":your_service" >

调试器已附加到您的主进程。当新的启动时,它没有附加调试器,所以它会忽略你的断点。

【讨论】:

在应用程序中添加了它,但是当我调试时仍然没有输入onCreate 尝试删除行 android:process=":your_service"

以上是关于未调用 Android 服务的主要内容,如果未能解决你的问题,请参考以下文章

Android WebViewClient onReceivedError 未调用 404 错误

android O中的onMessageReceived在后台应用时未调用

从 Android 后台服务问题获取当前位置

在 Android 中未调用 onlocationchanged

未调用 Android BLE onCharacteristicChanged()

未调用 Android onBackStackChanged()