【问题标题】:Application not starting on startup应用程序未在启动时启动
【发布时间】:2023-09-18 02:46:01
【问题描述】:

我的 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 设备启动时,都会显示一条通知说应用程序权限被拒绝或类似的东西..

不管有没有,问题是应用程序不会在启动时启动。

我想知道为什么..

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

【问题讨论】:

  • 尝试将 android.intent.action.MAIN 替换为 com.tominocz.stonequestapp.MainActivity 并将 android.intent.category.LAUNCHER 替换为 android.intent.category.DEFAULT,发布您的 logcat 以获得许多建议
  • 哦,等等,我想我知道这里出了什么问题。也许在 StartOnBoot.java 文件中,我应该删除 if 语句并扫描系统是否已完成启动。
  • StartOnBoot.java 应该是普通的类文件然后......就像没有扩展的东西......
  • 我没有发布整个代码,因为原始代码不是用于启动应用程序..
  • 然后把 logcat 和 MainActivity 扩展 Acvtivty 对吗?您是否尝试过我所说的替换?

标签: java android boot permission-denied


【解决方案1】:

试试这个。

<?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

【讨论】:

    最近更新 更多