应用程序无法在启动时启动

时间:2015-07-12 15:12:24

标签: java android boot permission-denied

我在Manifest.xml文件中有这个:

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="22" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="nosensor"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.tominocz.stonequestapp.StartOnBoot"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="nosensor" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PlayerListActivity"
            android:launchMode="singleTask"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:launchMode="singleTask"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我在StartOnBoot.java文件中有这个:

修改: 这是我有的实际StartOnBoot.java文件:

package com.tominocz.stonequestapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.tominocz.stonequestapp.config.Config;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

@SuppressWarnings("deprecation")
@SuppressLint("SdCardPath")
public class StartOnBoot extends BroadcastReceiver {

    public static String Latest;
    public static String previous;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            if (new File("/data/data/com.tominocz.stonequestapp/app.properties").exists()) {
                Config.load();

                if (MainActivity.StartOnBoot = true) {
                    getLatestVersion(context);
                    if (MainActivity.EnableNotifications = true) {
                        triggerUpdateNotification(context, "New version " + Latest + " released!");
                    }
                }
            }
        }
    }

    public static void getLatestVersion(Context ctx) {
        try {
            String myUri = "http://www.stonequest.de/version.php";
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(myUri);

            HttpResponse response = httpClient.execute(get);

            String ver = EntityUtils.toString(response.getEntity(), "UTF-8");
            if (ver != null) {
                Latest = ver;
                if (MainActivity.EnableNotifications = true) {
                    if (previous != Latest) {
                        triggerUpdateNotification(ctx, "New version " + Latest + " released!");
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    public static void loadLastVersion() {
        try {
            FileInputStream fis = new FileInputStream(new File("/data/data/com.tominocz.stonequestapp/Last.ver"));
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            previous = br.readLine();
            fis.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void triggerUpdateNotification(Context ctx, String s) {
        CharSequence title = "StoneQuest just got updated!";
        CharSequence message = s;
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) ctx.getSystemService("notification");
        Notification notification;
        notification = new Notification(com.tominocz.stonequestapp.R.drawable.ic_launcher, "New version released!",
                System.currentTimeMillis());
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, null, 0);
        notification.setLatestEventInfo(ctx, title, message, pendingIntent);
        notificationManager.notify(1010, notification);
    }
}

我不知道我的应用程序是否会这样做,但每当Android设备启动时,会显示一条通知,说明应用程序权限被拒绝或类似的内容会显示出来..

无论是否有,问题是该应用程序不会在启动时启动。

我想知道为什么......

有人知道为什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

试试这个。

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="22" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="nosensor"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.tominocz.stonequestapp.StartOnBoot"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="nosensor" >
            <intent-filter>
                <action android:name="com.tominocz.stonequestapp.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PlayerListActivity"
            android:launchMode="singleTask"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="com.tominocz.stonequestapp.PLAYERLISTACTIVITY" />
                <action android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:launchMode="singleTask"
            android:screenOrientation="nosensor"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="com.tominocz.stonequestapp.SETTINGSACTIVITY" />
                <action android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

将此添加到onReceive()

  Intent start = new Intent(getApplicationContext(), MainActivity.class);
  startActivity(start);

确保MainActivity扩展Activity