I have a BroadCastReceiver like this:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(Arrays.toString(intent.getExtras().keySet().toArray()));
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angryyogurt.yotransfer">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<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=".NewIncomingReceiver">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
android:priority="1010"/>
<action
android:name="android.intent.action.NEW_OUTGOING_CALL"
android:priority="1010"/>
<action
android:name="android.intent.action.PHONE_STATE"
android:priority="1010"/>
</intent-filter>
</receiver>
</application>
</manifest>
I use this to listen to the SMS. When I try to send message to this phone, it prints the key set [format, pdus, slot, phone, subscription].
But when I debug it and set the breakpoint at System.out.println..., I see that intent.mExtras.mMap is null. Why is this? How to check items of the map in intent during debugging?
The instance variable mMap is only populated after the extras are unmarshalled (unparceled, deserialized). If you haven't done anything to cause the extras to be unmarshalled, mMap will be null.
Try this:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean b = intent.hasExtra("blah");
System.out.println(Arrays.toString(
intent.getExtras().keySet().toArray()));
}
}
calling hasExtra() will force the extras Bundle to be unmarshalled. Set your breakpoint on the System.out.println() and you should see the extras in mMap.
Related
I have a BroadcastReceiver that is not being instantiated or called, any help on what I'm doing is appreciated.
It should be responding to wifi connection/disconnection events but isn't, and it's superclass constructor is not even being called either.
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
BroadcastReceiver:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "action: " + intent.getAction());
Log.v(TAG, "component: " + intent.getComponent());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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=".ConnectivityChangeReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
The <action> name is set incorrectly. It should be: android.net.conn.CONNECTIVITY_CHANGE. See the "Constant Value" as described here.
In my application I am trying to receive broadcast Media_Scanner_Finished. But the receiver is not getting called.
Here is my code-
'<?xml version="1.0" encoding="utf-8"?>'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="objectdistance.ankeshkjaisansaria.ram.sita.MyApp">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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="objectdistance.ankeshkjaisansaria.ram.sita.myApp.broadcastreceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_FINISHED"/>
<action android:name="android.intent.action.MEDIA_SCANNER_STARTED"/>
<action android:name="android.intent.action.MEDIA_SCANNER_STARTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
</manifest>
In my Broadcast receiver class:-
public class broadcastreceiver extends BroadcastReceiver {
BroadcastReceiver mMediaScannerReceiver;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("INFO", "Enter BroadcastReceiver");
}
}
I think the issue is relating to permission required in manifest file to get access to Media_Scanner broadcast.
One more thing I would like to clear is that :- Does Media_Scanner_Started gets called when content provider Media Image database gets updated ?
Add the receiver in code rather than in the manifest:
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
filter.addDataScheme("file");
scannerStartedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
}
}
I followed a tutorial on Receiving sms using BroadcastReceiver
https://www.youtube.com/watch?v=h-zYXVODiPo
I used exactly the same codes. Enabled Telnet client. Does my device need to be rooted first? I don't know the problem. Please help.
Here's my MainActivity
public class MainActivity extends Activity {
BroadcastReceiver receiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arr0, Intent arr1) {
processReceiver(arr0,arr1);
}
};
registerReceiver(receiver,filter);
}
public void onDestroy(){
super.onDestroy();
unregisterReceiver(receiver);
}
public void processReceiver(Context context,Intent intent){
Toast.makeText(context,"RECEIVED",Toast.LENGTH_LONG).show();
TextView lbs = (TextView)findViewById(R.id.textView1);
Bundle bundle = intent.getExtras();
Object[] objArr = (Object[])bundle.get("pdus");
String sms="";
for(int i=0;i>objArr.length;i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objArr[i]);
String smsBody = smsMsg.getMessageBody();
String senderNumber = smsMsg.getDisplayOriginatingAddress();
sms+= "From: "+senderNumber+"\nContent: "+smsBody+"\n";
}
lbs.setText(sms);
}
Here's my Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gavadev.com.smsreceiver">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
</application>
</manifest>
Use a Broadcast Receiver which will not be unregistered when destroying the activity:
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
// your processing will go here...
}
}
}
Register the receiver via manifest file as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverpremier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<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" >
<activity
android:name="com.example.broadcastreceiverpremier.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="MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
When I create class that extends BrodcastReceiver and register it.
Error Unable to destroy activity, Receiver not registered: null onDestroy() function in MainActivity When call unregisterReceiver.
onCreate():
ConnectionChangeReceiver connectionChangeReceiver = new ConnectionChangeReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectionChangeReceiver, filter);
onDestroy():
#Override
protected void onDestroy() {
Log.v("YourActivity", "onDestroy");
unregisterReceiver(connectionChangeReceiver);
super.onDestroy();
}
ConnectionChangeReceiver Class:
public class ConnectionChangeReceiver extends BroadcastReceiver
{
#Override
public void onReceive( Context context, Intent intent )
{
Toast.makeText(context, "CONN", Toast.LENGTH_SHORT).show();
}
}
manifest: UPDATE: After adding <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mamrezo.mapapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mamrezo.mapapp.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ConnectionChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
I always register the receiver in the onStart and unregister it in the onStop. I knew this thanks to this answer : https://stackoverflow.com/a/24391706/1203797
If its still not working in your case, simply wrapunregisterReceiver(connectionChangeReceiver); a try-catch block would be the answer, as stated in the link i posted above.
Or you can register it in the manifest, example :
<receiver android:name=".ConnectionChangeReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I want to start an activity even before screen lock after rebooting. here is my code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.bootservicestartup.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=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
public class BootUpReceiver extends BroadcastReceiver {
#SuppressLint("InlinedApi")
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, MainActivity.class);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
what the code does is when the phone is rebooted, an activity will be shown. that works. but I also want the activity to be shown even before showing the screen lock.
Hope this helps you if you did it as a service;
You need the following in your AndroidManifest.xml file:
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
(you don't need the android:enabled, exported, etc., attributes... the Android defaults are correct)
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}