无法在启动时启动服务



我的应用程序有一个相机照片备份服务。当用户打开该服务时,它会自动将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);
    }
}

这是github 上启动服务错误的完整项目

我试着多次重启操作系统,但不喜欢打印任何日志!任何建议都将不胜感激。

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

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
   Logs("ON Boot completed with System BroadcastReceiver");
  //do your job
 }

并在manifest.xml 中将<intent-filter>设置为类似于您的OSBootReceiver

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

清单中正确的receiver条目应该只是

<receiver android:name=".OSBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
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");
}
}

相关内容

  • 没有找到相关文章

最新更新