BroadcastReceiver not working via AndroidManifest - android

I have some BroadcastReceiver named LocalTokenRequestReceiver in my app.
I'm trying to send broadcast from another app.
This approach works fine:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
/* ..... */
LocalTokenRequestReceiver localTokenRequestReceiver = new LocalTokenRequestReceiver();
IntentFilter localTokenRequestFilter = new IntentFilter(getString(R.string.local_token_request));
localTokenRequestFilter.addCategory(getString(R.string.broadcast_category));
registerReceiver(localTokenRequestReceiver, localTokenRequestFilter);
}
}
But it does not work this way:
<receiver
android:name=".LocalTokenRequestReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="#string/local_token_request" />
<category android:name="#string/broadcast_category" />
</intent-filter>
</receiver>
Why is this may be so? I'm testing my app on Android 4.2.

<intent-filter> does not support resources. Use explicit strings.
See <action> and <category>.

Related

android.net.wifi.STATE_CHANGE intent handling (in Activity)

I would like to received a notification of the change of state of WiFi connection in my Android application.
I found several suggestion (here on SO) that mostly leads to the definition of an intent.
I'm using this approach:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
checkWiFi();
}
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null)
checkWiFi();
}
};
That works but I'm wondering if I can use a Manifest centric approach i.e. if I put this in my AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</activity>
I should be notified of the incoming intent . Right?
How can I handle this in my Activity?
Best regards, Mike
You should create class
MyBroadcastReceiver extends BroadcastReceiver
and next Manifest
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>

Custom launcher not receiving custom broadcast

I have a customer that needs a custom launcher to make his devices like a kiosk, just because he needs the device have limited access to limited applications. Everything is ok so far.
One of the most important feature he wants is the possibility to increase/decrease the applications that can be launch in his devices. I wrote the GCM receiver and it's also ok.
My problem is: I want to send a custom broadcast from the GCM service to my main activity (the home activity). I think the code is OK, but the broadcast is never received in main activity. Any ideas?
My manifest:
<activity
android:name="jv.android.atmlauncher.activity.HomeScreen"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="jv.antroid.atmlauncher.apklistchanged" /> <!-- This is my custom broadcast -->
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My Service:
#Override
public void onHandleIntent(Intent intent) {
...
Intent intent = new Intent("jv.antroid.atmlauncher.apklistchanged");
sendBroadcast(intent);
}
My home activity:
private BroadcastReceiver listapkReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
registerReceiver(listapkReceiver, new IntentFilter("jv.antroid.atmlauncher.apklistchanged"));
listapkReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
... Do something.
}
};
}
#Override
protected void onDestroy() {
unregisterReceiver(listpkReceiver);
super.onDestroy();
}

android integrating library app

I am trying to integrate an open source app into my android app. I have made the open source app as a library app and integrated the xml into my android manifest file as well. There are no compile errors.
First Screen is the login screen for the library app and when it is called it is throwing java lang class exception error at:
m_app = (TodoApplication) getApplication();
source code of loginscreen.java:
public class LoginScreen extends Activity {
final static String TAG = LoginScreen.class.getSimpleName();
private TodoApplication m_app;
private Button m_LoginButton;
private BroadcastReceiver m_broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
m_app = (TodoApplication) getApplication();
// supposed to help with the banding on the green background
findViewById(R.id.loginbackground).getBackground().setDither(true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.todotxt.todotxttouch.ACTION_LOGIN");
m_broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TodoTxtTouch.class);
startActivity(i);
finish();
}
};
registerReceiver(m_broadcastReceiver, intentFilter);
m_LoginButton = (Button) findViewById(R.id.login);
m_LoginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
login();
}
});
//final RemoteClient remoteClient = m_app.getRemoteClientManager()
// .getRemoteClient();
//if (remoteClient.isAuthenticated()) {
switchToTodolist();
//}
}
private void switchToTodolist() {
Intent intent = new Intent(this, TodoTxtTouch.class);
startActivity(intent);
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(m_broadcastReceiver);
}
void login() {
final RemoteClient client = m_app.getRemoteClientManager()
.getRemoteClient();
if (!client.isAvailable()) {
Log.d(TAG, "Remote service " + client.getClass().getSimpleName()
+ " is not available; aborting login");
Util.showToastLong(m_app, R.string.toast_login_notconnected);
} else {
RemoteLoginTask loginTask = client.getLoginTask();
loginTask.showLoginDialog(this);
}
}
}
Integrated library code in android manifest.xml:
<activity android:name="com.todotxt.todotxttouch.LoginScreen" android:label="#string/app_label"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="com.todotxt.todotxttouch.category.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.todotxt.todotxttouch.TodoApplication" />
<activity android:name="com.todotxt.todotxttouch.Filter" android:label="Filter"
android:theme="#android:style/Theme.NoTitleBar" />
<activity android:name="com.todotxt.todotxttouch.Preferences" android:label="#string/set_preferences" />
<activity android:name="com.todotxt.todotxttouch.AddTask" android:label="#string/addtask"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShortcut"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/shortcut_addtask_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShare"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/share_addtask_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity-alias>
<activity android:name="com.todotxt.todotxttouch.HelpActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.todotxt.todotxttouch.TodoTxtTouch" android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Could anyone please help me on understanding the problem.
Let me explain further: I am having a file called Todoapplication.java....so the class exists...it being called from LoginScreen.java as
m_app = (TodoApplication) getApplication();
and that is where I am getting java lang class exception?
Activity.getApplication() returns an instance of the application class that was declared in the manifest in the <application> element. I don't see it in your pasted manifest.
It's not enough to simply have the application class in your app. It must be explicitly designated as one in the manifest.
I maybe getting the wrong end of the stick so I beg the programming gods for forgiveness in advance.
Assuming you are developing in Eclipse, is this not a simple case of having a project in Eclipse with the open source source in, which within the project properties has the option isLibrary ticked.
In YOUR project's properties you can add a library and Eclipse will list the open source one (and any others that have the "isLibrary" checked). Would you not simply select the open source project and add it. Your project will then add the library and build again?
To access the open source project, now a library, you can use "import" statements to access any public methods that are exposed.
A good example of this setup process using an open source library project is Actionbar Sherlock to which I wrote a tutorial youtube that demonstrates visually what I have just written. It can be found at http://www.youtube.com/watch?v=avcp6eD_X2k

Android broadcast receiver not working

I try to get a broadcast receiver working. Should be as simple as possible, I have my manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytest.intentRec" android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".mainAct" 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.mytest.intentRec.MyIntentRec"
android:enabled="true" >
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
As you can see I have a main activity mainAct, this does nothing but sending the broadcast once started:
public class mainAct extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.sendBroadcast(new Intent());
}
}
and I have a class MyIntentRec, which is as simple as it could:
public class MyIntentRec extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("IntentRec", "got it");
}
}
What I expect is that when I start my app that a broadcast is sent and being picked up and that a log entry is written. I don't see that log entry and I don't see any error. I'm suspecting to have either an error in the manifest or in sending the broadcast. I just created an empty intent there, does it need to be some intent with certain properties?
Please setClass for your Intent,
EX:
public class mainAct extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i=new Intent("any string");
i.setClass(this, MyIntentRec.class);
this.sendBroadcast(i);
}
}
That is what it means " The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name."
[Old answer]
You should register what kind of actions you need in the manifest.
Ex:
<receiver android:name="com.mytest.intentRec.MyIntentRec" android:enabled="true" >
<intent-filter>
<action android:name="your.intent" />
</intent-filter>
</receiver>
send it,
this.sendBroadcast(new Intent("your.intent"));
it is insufficient to make just new Intent();. You have to specify it with some action. Also, you have to specify in your manifest the intent filter for this particular action. Please read more here and here.
You didn't define any Intent Filters in the manifest for your BroadcastReceiver. Specify one for a custom Action type. You also have to define this custom Action type in the Intent you brodcast upon startup.
Try specifying what actions your receiver should catch in the manifest. You can do this as such:
<receiver android:name="com.mytest.intentRec.MyIntentRec">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

broadcast receiver never gets called

I'm trying to learn how Broadcast Receivers work but I don't succeed, my receivers are never called. Can you guys point me in the right direction?
Here's the code:
Main.java
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(getApplicationContext(), OutgoingReceiver.class);
intent.setAction("com.javaGroup.broadcastReceiver.views.TEST");
sendBroadcast(intent);
}
}
OutgoingReceiver.java
public class OutgoingReceiver extends BroadcastReceiver {
public static final String CUSTOM_INTENT = "com.javaGroup.broadcastReceiver.views.TEST";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("HIT OUTGOING-----------------------------------","");
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
}
}
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
Log.d("GOT THE INTENT-------------------------------","");
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaGroup.broadcastReceiver.views" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.javaGroup.broadcastReceiver.views.Main" 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=".OutgoingReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.javaGroup.broadcastReceiver.views.TEST"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Where I'm wrong?
You need to add the right permission in Manifest for your receiver . See list of permissions
Of course you're not going to get it, your BCR (namely OutgoingReceiver) isn't registered to listen to that Intent (com.javaGroup.broadcastReceiver.views.TEST) which your Activity (Main) is broadcasting to.
Note
You should write more comments in your code so that YOU can understand what you are doing. As it stands, I can only guess. You should understand the Activity lifecycle and the full behavior of what you are trying to do. By looking at your code, that's not exactly clear.

Categories

Resources