【问题标题】:Activity don't starts at boot completed活动不会在启动完成时开始
【发布时间】:2016-11-04 23:13:28
【问题描述】:

我有Activity

public class I1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    }

}

BroadcastReceiver

public class I2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent mIntent = new Intent(context, I1.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }

}

为什么我的活动没有在启动完成时开始?那是AndroidManifest.xml的代码

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

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="25" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >

        <activity
            android:name=".I1"
            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:name=".I2"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

及布局代码

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:background="#000000"
    tools:context="com.mypackage.Main" />

您能帮帮我吗,我的代码有什么问题?

【问题讨论】:

  • 请用操作系统指定设备名称。
  • 意图收到了吗?有什么例外吗?最后一个,该应用程序是否在 SD 卡上?
  • 没有记录任何异常。我使用USB调试logcat。我也尝试在手机和 SD 卡上安装应用程序。未收到意向。手机 - 飞利浦 Android 4.0.3。
  • 您可能希望使用完全限定名称而不是 android:name=".I1"

标签: android


【解决方案1】:

这个问题真是令人难以置信。我有两部相同的手机 - 飞利浦 Android 4.0.3。其中之一,我测试我的应用程序有 BOOT_COMPLETED 操作的错误。 这段代码是我认为最好的

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

和广播接收器

@Override
public void onReceive(Context context, Intent intent) {
    Intent mIntent = new Intent(context, I1.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mIntent);
}

它也有效!

感谢用户@AxelH!​​


添加。在某些手机(例如,ZTE Blade HN)上,您需要启动一次主要活动,因为如果应用程序是新的并且从未启动过,手机不会让 Broadcast Receiver 被激活被打开。即使在更新应用程序之后,您也需要这样做。

【讨论】:

    【解决方案2】:

    试试下面的

     <receiver android:name=".I2" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
    

    还有一点需要注意

    如果您的应用安装在外部存储设备(SD 卡)上,您将永远不会收到 Boot Complete 操作。所以你必须在清单标签中指定以下内容。

    android:installLocation="internalOnly"
    

    【讨论】:

    • 抱歉,还是不行。没啥事儿。我也移除了 SD 卡并在手机内存中重新安装了应用程序。
    • 我只会不同意never receive。这有效(当我进行测试时它有效)但是如果在安装卡之前发送意图,它不会发生变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2016-03-08
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    相关资源
    最近更新 更多