无法在启动时启动服务

时间:2014-09-16 09:53:17

标签: java android boo

我的应用程序有相机照片备份服务。当用户打开服务时,它会自动将SD卡照片备份到远程服务器。但是当用户关闭Android操作系统并再次启动时,备份服务无法再次启动。

我在线阅读了很多文章,我的描述与他们描述的完全相同,但结果却是错误的。 也许这里的一些专家可以帮助我。请参阅下面的代码。

在android manifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.seafile.seadroid2"
      android:versionCode="20"
      android:versionName="1.0.1"
      android:installLocation="internalOnly">              
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <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" />        
    <application
        android:allowBackup="true"
        android:name="com.seafile.seadroid2.SeadroidApplication"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">            
        <receiver android:name=".OSBootReceiver" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />  
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <activity android:name="com.seafile.seadroid2.BrowserActivity"
                  android:label="@string/app_name"
                  android:theme="@style/Theme.SeafileTheme"
                  android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.seafile.seadroid2.transfer.TransferService" > </service>
        <service android:name="com.seafile.seadroid2.monitor.FileMonitorService" > </service>
        <service android:name="com.seafile.seadroid2.sync.CameraUploadService" > </service>            
    </application>
</manifest>

OSBootReceiver类

 public class OSBootReceiver extends BroadcastReceiver {
    private static final String DEBUG_TAG = "OSBootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(DEBUG_TAG, "boot to notic receiver");

        Intent cameraUploadIntent = new Intent(context,
                CameraUploadService.class);
        SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        boolean isUploadStart = settings.getBoolean(
                BrowserActivity.CAMERA_UPLOAD_SWITCH_KEY, false);
        if (!isUploadStart) {
            return;
        }
        Log.d(DEBUG_TAG, "boot to start service");
        context.startService(cameraUploadIntent);
    }

}

这是带有启动服务错误on github

的完整项目

我试图多次重启os,但不喜欢打印任何日志!任何建议将不胜感激。

3 个答案:

答案 0 :(得分:1)

尝试检查onReceive(.....)中的引导已完成操作

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

   Logs("ON Boot completed with System BroadcastReceiver");

  //do your job

 }

并将<intent-filter>设置为OSBootReceiver

中的manifest.xml
       <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

答案 1 :(得分:0)

在您的清单中更正receiver条目应该只是

<receiver android:name=".OSBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

答案 2 :(得分:0)

What is package name used in file OSBootReceiver.java

If package name is  com.seafile.seadroid2,


**change Receiver in manifest as below:**

<receiver android:name="com.seafile.seadroid2.OSBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

I added like above, and i am getting Boot completed broadcast intent.

For testing purpose, 
add Toast message in onRecieve method:


    public class OSBootReceiver extends BroadcastReceiver {
    private static final String DEBUG_TAG = "OSBootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "boot completed", Toast.LENGTH_SHORT).show();
    Log.e(DEBUG_TAG,"boot completed intent received");
}
}