Android启动服务启动时,无法接收广播

时间:2013-10-03 10:35:08

标签: java android android-service android-broadcast

仅当用户按下刷新按钮时,我的服务才会在系统启动时启动。我已经尝试了堆栈中的每一个搜索但没有任何我已经重写了我的代码4次没有,我希望我的服务在一个时间间隔运行,当按下刷新按钮时,我不明白为什么不在启动时运行,我错过了什么吗?

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.moody"
android:installLocation="internalOnly"
android:versionCode="0"
android:versionName="0.6.7.2 alpha" >

<permission
    android:name="com.android.moody.permission.GET_SERVER_DATA"
    android:protectionLevel="normal" />

<uses-sdk
    android:maxSdkVersion="18"
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission android:name="com.android.moody.permission.GET_SERVER_DATA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:allowClearUserData="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="activities.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="activities.Menu_esq"
        android:label="@string/title_activity_menu_esq" >
    </activity>
    <activity
        android:name="activities.BaseActivity"
        android:label="@string/title_activity_base" >
    </activity>
    <activity
        android:name="activities.MainView"
        android:label="@string/title_activity_main_view" >
    </activity>
    <activity
        android:name="activities.LoginActivity"
        android:label="@string/app_name"
        android:noHistory="true"
        android:windowSoftInputMode="adjustResize|stateVisible" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.moody.LeftActivity"
        android:label="@string/title_activity_left" >
    </activity>
    <activity
        android:name="com.example.moody.RightActivity"
        android:label="@string/title_activity_right" >
    </activity>
    <activity
        android:name="activities.UserDetailsActivity"
        android:label="@string/title_activity_user_details" >
    </activity>
    <activity
        android:name="fragments.FragTopicsPreview"
        android:label="@string/title_activity_copy_of_topics_preview" >
    </activity>
    <activity android:name="activities.LoadingActivity" >
    </activity>

    <service
        android:name="service.ServiceBackground"
        android:enabled="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/moody_service" >
    </service>

    <receiver android:name="service.Alarm" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="service.StartServiceReceiver"
        android:permission="com.android.moody.permission.GET_SERVER_DATA" >
        <intent-filter>
            <action android:name="moody_get_data" />
        </intent-filter>
    </receiver>
</application>

ServiceBackground

public class ServiceBackground extends Service {
Alarm alarm = new Alarm();

public ServiceBackground() {
    // TODO Auto-generated constructor stub
}

private boolean isRunning = false;
Object getContent;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub

    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    // android.os.Debug.waitForDebugger();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    // Starts the alarm
    alarm.setAlarm(getApplicationContext());

    // Announcement about starting
    Log.d("service", "Service Started");

    // Start a Background thread
    isRunning = true;
    Thread backgroundThread = new Thread(new BackgroundThread());
    backgroundThread.start();

    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    // Stop the Background thread
    isRunning = false;

}

private class BackgroundThread implements Runnable {

    public void run() {
        try {
            while (isRunning) {
                Log.d("service", "Thread started");
                new ManContents().getAll(getResources(),
                        getApplicationContext());
                isRunning = false;
            }
            stopSelf();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

警报

public class Alarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    context.startService(new Intent(context, ServiceBackground.class));
}

public void setAlarm(Context context) {
    String alarm = Context.ALARM_SERVICE;
    AlarmManager am = (AlarmManager) context.getSystemService(alarm);

    Intent intent = new Intent("moody_get_data");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
    // long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    long interval = 1000 * 60 * 2;
    long triggerTime = SystemClock.elapsedRealtime() + interval;

    am.setRepeating(type, triggerTime, interval, pi);

}

public void CancelAlarm(Context context) {
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent
            .getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

}

StartServiceReceiver

public class StartServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("service", "Starting service from serviceReceiver");

    Intent service = new Intent(context, ServiceBackground.class);
    context.startService(service);
}

}

注意

  • 我已经尝试过没有过滤器直接启动服务,但是 没有理由不开始,所以使用过滤器是唯一的方法 工作
  • 我也试过没有StartServiceReceiver.java,意图在警报内但没有运气,如果有人认为这是问题,我可以再次更改代码。
  • 我在线程中的while(isRunning)仅用于将来的目的,但我认为问题不在于此。

1 个答案:

答案 0 :(得分:3)

与我的一个实现相比,清单中的差异很小:

  • 使用的权限(似乎与你的相同)

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  • 接收者

    <receiver
        android:name="com.<!your activity!>"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    
Yoiu看到有关该许可的额外条目。