我的英文很差。我無法在啓動時啓動安卓服務,但我不知道問題所在。我正在嘗試示例代碼,但沒有成功。有人可以給我一個運行Java的項目嗎?其他代碼適用於其他人,但在我的平板電腦智能手機模擬器上不起作用。 Android 4.0中是否存在問題?開始Android服務4.0開機時間
這是我的代碼:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service22"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
>
<supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<service android:name="com.example.MyService">
<intent-filter>
<action android:name="com.example.MyService">
</action>
</intent-filter>
</service>
<receiver android:name="com.example.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
public class MyService extends Service
{
private static final String LOG_TAG = "::Monitor";
@Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
for (int i = 0 ; i < 20 ; i++)
{
mensaje();
}
Log.e(LOG_TAG, "Service started.");
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
@Override
public IBinder onBind(Intent intent)
{
Log.e(LOG_TAG, "Service bind.");
return null;
}
public void mensaje()
{
Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show();
}
}
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
}
String LOG_TAG = "::StartAtBootServiceReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
}
}
}
[在啓動時Android 4.0上啓動Android服務]的可能重複(http://stackoverflow.com/questions/11640142/start-android-service-on -boot-time-android-4-0) – CommonsWare 2012-07-24 23:44:01
[嘗試在Android上啓動服務時啓動服務](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on -boot-on-android) – user3439968 2014-09-24 21:17:21