Is it mandatory to have broadcast receiver as a separate file? - android

I have declared an activity and a broadcast receiver in my manifest file, however, I have the code for onReceive() in my activity as a separate public class. When I try to trigger a broadcast from the adb command line I get a classnotfound error.
My question is, is it mandatory to have the broadcastreceiver as a separate class in a separate file ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityAsDialogActivity.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
:
:
}
public class TestEmail extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MYINTENT)){
Log.d("Manju ==>","ActivityAsDialogActivity, Got the intent"+MYINTENT);
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
}//end of if statment
}//end of onReceiver
}//end of Broadcast
}//end of class ActivityAsDialogActivity
below is manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mot.activityasdialog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ActivityAsDialogActivity"
android:excludeFromRecents="true"
android:theme="#style/EmptyActivity"
android:configChanges="keyboardHidden|orientation|screenSize|uiMode">
<!-- android:theme="#android:style/Theme.Holo.Dialog"
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="com.mot.activityasdialog.ActivityAsDialogActivity.TestEmail" android:enabled="true">
<intent-filter>
<action android:name="com.mot.MANJU"></action>
</intent-filter>
</receiver>
</application>
</manifest>

Short answer: Yes it needs to be a different Class. And in a separate file.
Long answer: Since your BroadcastReceiver must be a subclass of BroadcastReceiver and your Activity must be a subclass of Activity and Java does not allow multiple inheritance and Activity and BroadcastReceiver are not related in a suitable fashion. Yes, it has to be two classes.
It seems to have to be in its own file too. I tried it and the system cannot find the BroadcastReceiver if it is an inner class. public or not.

Related

launch android app on device startup

I am trying to launch my application when I turn my android device on. Currently I have a BroadcastReciever class and a Service class however the app does not seem to launch when I reboot my device.
My BroadCast Receiver Class
public class Bootup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, AutoStart.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
}
}
}
My Service class
public class AutoStart extends Service {
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:sharedUserId="android.uid.system">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".Bootup"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".AutoStart">
<intent-filter>
<action android:name="com.example.test.AutoStart"></action>
</intent-filter>
</service>
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Result"
android:label="#string/title_activity_result"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.test.MyActivity" />
</activity>
<service
android:name=".functionality"
android:enabled="true" />
</application>
</manifest>
Could someone point me in the right direction. I am not sure what I am doing wrong.
Your application needs to be manually run at least once before boot receiver start working, also boot intent might be send after quite some time and maybe even intercepted and killed by other apps. I encountered such behaviour with few third party sms apps.
I'm not entirely sure about this, but try removing the android:exported="false" from the BootBroadcastReceiver. android:exported="false" means no other app can call it, and the system is just as an app in here.
Try using context.startActivity(i); instead of context.startService(i);.
You should correct this line in your BroadCast Receiver Class:
Use: context.startActivity(i);
Instead of: context.startService(i);

About android.intent.action.BOOT_COMPLETED [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I write a sample to learn BroadcastReceiver..But When I reboot my phone, the app broke down. Here is my source code and manifest.xml:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class BootCompleteReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<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:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You do not have a class named com.example.boot.BootCompleteReceiver, at least based on the code that you have shown in your answer.
While you have a class named BootCompleteReceiver:
It is an inner class of MainActivity, and so BootCompleteReceiver is not named com.example.boot.BootCompleteReceiver
It is a regular inner class of MainActivity (no static keyword), and so Android could not create an instance of it anyway, even if you had the right name in the manifest
Either move BootCompleteReceiver to be a regular standalone Java class in its own .java file, or make it be a public static class inside MainActivity. In the latter case, the fully-qualified class name for your manifest entry would be com.example.boot.MainActivity$BootCompleteReceiver.

Android application without GUI

I have been developing a simple application without UI using broadcast receiver.
The app doesn't contain any ACTIVITIES.
I have given necessary permissions.
I took the code from this url:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html
The app shows a toast "Not connected to internet" when I click change wifi state. It's working correctly.
But my question is There is an activity registered in my manifest file which I don't have. So I delete those lines from my manifest. Then no toasts are shown and I checked the logs too. No output on changing wifi state.
Why this happened? Please help me guys...
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_internetcheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcast_internetcheck.MainActivity"
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="com.example.broadcast_internetcheck.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my Broadcastreceiver class:
public class NetworkChangeReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
/*Above line will return the status of wifi */
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}
}
You will need to create a dummy activity for a Service which will be triggered in the onCreate() of the dummy, maybe a non-UI with finish() .
Without that the required implementation is not possible, esp above Android 3.1.
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Run only a background service when application start
Start android application without activity
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
And for more on Service:
http://developer.android.com/guide/components/services.html
https://developer.android.com/training/run-background-service/create-service.html
http://www.vogella.com/tutorials/AndroidServices/article.html
You can use service instead. But showing Toast through service bit complicated instead you can show notification through service for No Internet Connection.
If you don't want any activity check this answer. Actually you would have to create service for this: link
Create a transparent activity. Launch the toast while the activity is active and immediately finish the activity.

Broadcast Receiver Not working in 4.1.1

I have a broadcast receiver for incoming call.I want to launch a new activity when an incoming call comes.I am aware of the changes that are made from android 3.0,that the broadcast receiver will not work unless user manually starts an application
For that purpose I launch a dummy activity with just a toast message in it.Still the broadcast receiver is not working.
Here is my code
My broadcastreceiver
public class IncomingCallResult extends BroadcastReceiver
{
String TAG="IncomingCallResult";
#Override
public void onReceive(Context arg0, Intent I1)
{
Log.i(TAG,"inside on receive........");
Bundle bundle=I1.getExtras();
String state=bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent flash_intent=new Intent(arg0,LedFlasher.class);
flash_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(flash_intent);
}
}
}
manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blinker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".IncomingCallResult"
android:enabled="true">
<intent-filter
android:priority="214783648"
android:name="android.intent.action.PHONE_STATE">
</intent-filter>
</receiver>
<activity
android:name=".LedFlasher"
android:label="#string/title_activity_incoming_call_result" >
</activity>
<activity
android:name=".Dummy">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
What is wrong with the code?
Please help
As you have already known that the broadcast receiver will not work unless user manually starts an application For that purpose, you should not be surprised that your broadcast receiver will not work until user Manually open your application once. That is to say, you have to make a launcher activity which user is able to click and open it manually.
And what's more, it is better to Open and stay in your application for like 20s since I remember that the change of application configuration will take 10 or 20s to be saved.

How to create an android app with only 1 broadcastreceiver?

I am trying to create an application in Android that is composed of only 1 broadcastreceiver (and nothing else).
The broadcastreceiver should simply catch the broadcast(for example sms message received,log the info and finish).
However, I noticed that broadcast is not caught by the receiver, unless I indicate I have main Activity as the following AndroidManifest.xml will show:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.myapp.MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.myapp.MainActivity"
android:label="#string/activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I don't even have to have an Activity class within the application.
Also, if I remove either android.intent.category.LAUNCHER or android.intent.action.MAIN in the intent filter, it does not worrk either.
The behavoir is the same on my phone and the emulator which are both running android 4.2
my Broadcastreceiver class looks like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getAction(),Toast.LENGTH_SHORT).show();
}
}
Is it not possible to have an app with only a broadcastreceiver?
Starting from Android 3.1 (API 12), app cannot receive broadcasts until a UI component of an app (an Activity) has been manually opened by the user at least once. Even if user force stop the application , same is applied.
Reference : http://developer.android.com/about/versions/android-3.1.html#launchcontrols
SHouldn't you make a service instead of an application ? (I've never created a service, but i think it should be more appropriate in your case)

Categories

Resources