In: Computer Science
I am working on creating a Broadcast Receiver. I am extremely new to Android development and Java. I added my code at the bottom. Whenever I press the button the app crashes. I'm assuming something is wrong with connecting the broadcastIntent() function. I appreciate any help :)
Here are the directions from my professor:
public void broadcastIntent(View view){
Intent intent =
new Intent();
intent.setAction("my.CUSTOM_INTENT");
sendBroadcast(intent);
}
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
<intent-filter> <action android:name="my.CUSTOM_INTENT"></action> </intent-filter>
MY CODE:
<main activity>
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button mybutton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mybutton = (Button) findViewById(R.id.mybutton); broadcastIntent( ); } private void broadcastIntent() { Intent intent = new Intent(); intent.setAction("my.CUSTOM_INTENT"); sendBroadcast(intent); } }
<activity xml>
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="235dp" android:layout_marginEnd="146dp" android:layout_marginRight="146dp" android:onClick="broadcastIntent" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
<manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ica4_broadcast"> <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/Theme.ICA4Broadcast"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver" android:exported="true"> <intent-filter> <action android:name="my.CUSTOM_INTENT"/> </intent-filter> </receiver> </application> </manifest>
<my receiver>
package com.example.ica4_broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected" + intent.getAction(), Toast.LENGTH_LONG).show(); } }
As far as I can see there is no error in Code
What makes the app to crash?? Yes there could be certain mistakes out of which even one could crash ap
Please check the follwing points:
1. If you have not defined my.CUSTOM_INTENT define it as
public static final String CUSTOM_INTENT = "my.custom.intent.action.TEST";
Necessary to have action (It may be TEST,BLOCK etc)
2. If you have already defined make the following changes in manifest.xml flle.
<receiver android:name=".MyReceiver" android:exported="true">
<intent-filter>
<action android:name="my.CUSTOM_INTENT">
</action>
<action android:name="android.intent.action.TEST" >
</action>
</intent-filter>
</receiver>
3. May be error is due to formatting (very very less chance). Please just try once to have onRecieve() like
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String text="Intent Detected";
text+=intent.getAction();
Toast.makeText(context,text, Toast.LENGTH_LONG).show();
}
}