我正在開發我的第一個Android服務,我希望它在操作系統啓動時啓動。我在代碼中添加了一些打印輸出以檢測並確保接收器/服務被調用。那麼,我沒有看到它在logcat中。這是代碼,我想我做的一切都對嗎?或不?Android:接收器未被調用且服務未啓動
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.denniss"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name">
<activity android:name="Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:process=":random_number_background"
android:name=".RandomNumberService"
android:label="Random Number Service"/>
<receiver android:name="RandomNumberBroadcastReceiver"
android:process=":random_number_background">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
是應該產生隨機數的服務=)
package com.denniss;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class RandomNumberService extends Service{
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
廣播接收機
package com.denniss;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class RandomNumberBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("**************************************************");
System.out.println("RandomNumberBroadcastReceiver onReceive is called!");
context.startService(new Intent(context, RandomNumberService.class));
}
}
這個問題在這裏已經有很多次的問題了,搜索會在下面的答案中找到關於缺少的權限的答案。 – cistearns 2012-03-14 17:18:49