这是我的服务:
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。为什么呢?
答案 0 :(得分:3)
您必须将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" >
调试器已附加到主进程。当新的启动时,它没有附加调试器,因此它将忽略您的断点。