RECEIVE_BOOT_COMPLETED不起作用

时间:2015-01-08 13:24:28

标签: android android-manifest bootcompleted

我试图在android设备启动后写入文件。我在这里检查了与此主题相关的所有内容,但它们都没有为我工作。我的手机是华为荣誉(H30-U10),它已经扎根了。 Android版本4.2.2。这是我的代码:

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bog.ddrc.technicianmobile"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

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

    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name="bog.ddrc.technicianmobile.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="bog.ddrc.technicianmobile.activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:enabled="true"
            android:name="bog.ddrc.technicianmobile.StartTMServiceAtBootReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

StartTMServiceAtBootReceiver.java:

public class StartTMServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String data = "text";
        File path = Environment.getExternalStorageDirectory();
        File file = new File(path, "test.txt");

        try {
            OutputStream os = new FileOutputStream(file);
            os.write(data.getBytes());
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

删除

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

来自<receiver>区块。

答案 1 :(得分:0)

在尝试了所有提到的答案和技巧之后,我终于找到了为什么代码在我的手机中不起作用。一些Android手机,如&#34;华为Honor 3C Android 4.2.2&#34;在他们的设置中有一个Statup Manager菜单,您的应用必须在列表中检查。 :)