广播接收器 - 如何?

Broadcast receiver - how to?

我只是在练习,我正在尝试在 Android Studio 中制作简单的 brodcastreceiver。

我有这个清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.maurelio.flipcover">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name="SensorReceiver">

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

    </receiver>

</application>

我有这个SensorReceiver.java

package org.maurelio.flipcover;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by maurelio on 22/03/18.
*/

public class SensorReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Intent Detected.", 
    Toast.LENGTH_LONG).show();
  }
}

当我尝试 运行 我得到的应用程序时:"Default Activity not found"。 我也试过使 chache/restart 无效,没有任何变化。

我做错了什么?

感谢和问候。

这是因为您没有在 manifest.xml 文件中定义默认值 activity。

通常你会在那里定义一个 MainActivity。

在您使用的教程中,他们的清单中没有 MainActivity,但我认为这是他们的错误。但是,它们确实描述了 MainActivity class 本身。

我建议使用 Android Studio 从空白模板创建一个新项目,例如 "Basic Activity",然后慢慢将您的代码添加到该新项目中。

在您当前的项目中创建一个空的 activity 并将以下部分添加到您的清单中

 <activity android:name=".YourActivityName">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

用您的新 activity

更改 activity 名称