Search code examples
androidandroid-broadcastreceiver

Broadcast receiver - how to?


i'm just practing and i'm trying to make simple brodcastreceiver in Android Studio.

I have this manifest:

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

and i have this 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();
  }
}

When i try to run the app i get: "Default Activity not found". I tried also invalidate chache/restart, nothing change.

What i'm doing wrong?

Thanks and regards.


Solution

  • This is because you do not have a default activity defined in your manifest.xml file.

    Normally you would have a MainActivity defined there.

    In the tutorial you used, there is no MainActivity in their manifest, but I think that is their mistake. However, they do describe the MainActivity class itself.

    I would suggest using Android Studio to create a new project from a blank template such as "Basic Activity" and then slowly adding your code to that new project.