Unable to instantiate receiver has no zero argument constructor - android

I have some Problem with using BroadCastReceiver.Here comes the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhang.notificationtest">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationActivity"></activity>
<receiver android:name=".NotificationActivity$SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
and part of my codes
public class NotificationActivity extends AppCompatActivity {
public TextView textViewFrom, textViewContent;
public IntentFilter intentFilter;
public SMSReceiver smsReceiver;
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
textViewContent = (TextView)findViewById(R.id.textViewContent);
textViewFrom = (TextView)findViewById(R.id.textViewFrom);
intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
smsReceiver = new SMSReceiver();
registerReceiver(smsReceiver,intentFilter);
}
public class SMSReceiver extends BroadcastReceiver{
public SMSReceiver(){
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] smsMessages = new SmsMessage[pdus.length];
for (int i = 0; i < smsMessages.length; i++)
smsMessages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String from = smsMessages[0].getOriginatingAddress();
StringBuilder content = new StringBuilder("");
for (SmsMessage element: smsMessages)
content.append(element.getMessageBody());
textViewFrom.setText(from);
textViewContent.setText(content.toString());
}
}
}
could anyone offer me some help and tell me why it happened?Thanks a lot!

Your SMSReceiver is a non-static instance class. That means it can only be constructed in the context of the holding class, NotificationActivity. That's fine, but you can't register it in your manifest since the system would need to construct a NotificationActivity in order to instantiate your receiver to handle the broadcast.
BTW, either register the receiver in the manifest, or register it in your activity, but not both.

SMSReceiver is a non-static inner class, make it static or move it out of NotificationActivity if you want to keep the receiver in the manifest.
When you add the BroadcastReciever to the manifest, you are declaring that it is always registered (so you don't need the register and unregister in your activity)
If you do want to keep it in the manifest, the life cycle of an Activity and BroadcastReceiver are different, so it would be dangerous to access textViewFrom as you are currently doing.
If you want the receiver to be triggered only when the activity is 'alive', remove it from the manifest

Related

Broadcast receiver is not working - why so?

I'm studying Android.
I try to implement a Custom static Broadcast Receiver but it is not working.
I search for some issue from Google but I can't find something to solve this.
I work on Android 7.0 Min Level 24 Target Level 28
In fact, MyStaticReceiver isn't launching when I start the Activity (no log)
MyDynamicReceiver work perfectly
Do you have a solution?
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.receiver">
<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">
<activity android:name="test.receiver.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyStaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="#string/StaticAction" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java :
public class MainActivity extends Activity {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
private MyDynamicReceiver dynamicReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName receiver = new ComponentName(this, MyStaticReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
#Override
protected void onResume() {
super.onResume();
if (Debug) Log.i(TAG, "MainActivity:onResume");
configureDynamicReceiver();
}
#Override
protected void onDestroy(){
super.onDestroy();
if (Debug) Log.i(TAG, "MainActivity:onDestroy");
unregisterReceiver(dynamicReceiver);
}
public void onClickButton(View v) {
if (Debug) Log.i(TAG, "MainActivity:onClickButton");
Intent staticIntent = new Intent();
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
Intent dynamicIntent = new Intent();
dynamicIntent.setAction(getString(R.string.DynamicAction));
sendBroadcast(dynamicIntent);
}
public void configureDynamicReceiver() {
if( dynamicReceiver == null ) {
dynamicReceiver = new MyDynamicReceiver();
}
IntentFilter filter = new IntentFilter(getString(R.string.DynamicAction));
registerReceiver(dynamicReceiver, filter);
}
}
MyStaticReceiver.java (MyDynamicReceiver is the same ...)
public class MyStaticReceiver extends BroadcastReceiver {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
public MyStaticReceiver() {
if (Debug) Log.i(TAG, "MyStaticReceiver");
}
#Override
public void onReceive(Context context, Intent intent) {
if (Debug) Log.i(TAG, "MyStaticReceiver:onReceive");
}
}
You are actually sending an implicit broadcast, therefore the receiver declared in the manifest will not work.
If your app targets Android 8.0 (API level 26) or higher, you cannot use the manifest
to declare a receiver for most implicit broadcasts (broadcasts that
don't target your app specifically). You can still use a
context-registered receiver when the user is actively using your app. Link
You don't face any issue with MyDynamicReceiver because it is context-registered receiver.
But to make it work for MyStaticReceiver, you can try sending an explicit broadcast by passing the component name in the constructor of the Intent.
Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);

Broadcast Receiver for RECEIVE_SMS is not working when declared in manifest(statically)

I am trying to detect SMS received and read it via texttospeech.
when i declare the Broadcast Receiver in manifest it doesn't work. But it works when done dynamically in an activity.
I am aware that some Broadcast actions cant be caught in a receiver when declared in manifest and requires an activity(as mentioned here), but have seen people using RECEIVE_SMS in manifest as in here.
I don't know what I am doing wrong. Any help would be greatly appreciated!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bulsy.smstalk1">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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="com.bulsy.smstalk1.SmsListener"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS"
android:exported="true">
<intent-filter android:priority="2147483647">//this doesnt work
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
SmsListener.java
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
TextToSpeech textToSpeech;
String msg_from;
public SmsListener()
{
}
#Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
final String msgBody = msgs[i].getMessageBody();
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
String fromName = getContactName(context,msg_from);
fromName = fromName==null? msg_from:fromName;
textToSpeech.speak("You have a text message from " + fromName + ". Content: " + msgBody , TextToSpeech.QUEUE_FLUSH, null);
}
}
}
);
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsListener smsListener = new SmsListener();//Dynamically setting the receiver. this works.
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(smsListener,filter);
}
}
The root of the problem here is the lifetime of a manifest-registered Receiver instance. An instance of such a Receiver will only be alive until the onReceive() method completes. The TextToSpeech object will not be ready before the Receiver dies, and without any other indication of the Receiver working, it appears as though the Receiver has just failed.
The solution is to move the TextToSpeech functionality to a Service you can run from the Receiver, and pass the necessary info as extras on the Intent used to start it.

BroadcastReceiver onReceive() not Called when registered dynamically

The function "onReceive" is called when BroadcastReceiver is Registered in the Manifest but NOT called if registered dynamically.
The code that works is below:
public class EyeGesture extends BroadcastReceiver {
//Eye Gesture
private static IntentFilter eyeGestureIntent;
private static Context eyeGestureContext;
private static StringBuilder gestureInfo = null;
private static BroadcastReceiver broadcastReceiver;
// public void startEyeListening() {
//Eye Gesture
//}
#Override
public void onReceive(Context context, Intent intent) {
// this = context;
if (intent.getStringExtra("gesture").equals("WINK")) {
Log.e("WINKED ","");
}else {
Log.e("SOMETHING", "is detected " + intent.getStringExtra("gesture"));
}
//Disable Camera Snapshot
// abortBroadcast();
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntent = null;
eyeGestureContext = null;
gestureInfo = null;
}
}
Below is the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.inno.glassplugin" >
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainFunct"
android:icon="#drawable/ic_glass_logo"
android:label="#string/title_activity_main_funct" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger" />
</activity>
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
</application>
</manifest>
The problem is that "onReceive" is NOT called when registered dynamically. I have to do this in a dynamic way.
Below is the code that is NOT working code.
public class EyeGesture extends Activity {
//Eye Gesture
IntentFilter eyeGestureIntentFilter;
Context eyeGestureContext;
BroadcastReceiver broadcastReceiver;
public EyeGesture(){
Log.e("CONSTRUCTOR ", "");
eyeGestureContext = MainFunct.getCurrentContext();
eyeGestureIntentFilter = new IntentFilter("com.google.glass.action.EYE_GESTURE");
eyeGestureIntentFilter.setPriority(1000);
startRunning();
}
void startRunning(){
eyeGestureContext.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Received ", " Something");
}
},eyeGestureIntentFilter);
}
#Override
public void onResume(){
super.onResume();
}
#Override
public void onPause(){
super.onPause();
unregisterReceiver(broadcastReceiver);
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntentFilter = null;
eyeGestureContext = null;
}
}
Also, I don't want to extend BroadcastReceiver from this class. Why am I not receiving anything if registered dynamically. I also removed the following line from the Manifest:
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
but still, it is not working. There is no error or exception thrown.
What am I doing wrong?
Are you using explicit intent? It seems that dynamically registered broadcast receivers cannot receive explicit intents. Implicit intents work.
For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
If the issue is not explicit intents but if you are using LocalBroadcastManager for sendBroadcast then make sure that the registerReceiver is also called of LocalBroadcastManager and not of Context
Try using ApplicationContext instead of Activity.
Modyifing line:
eyeGestureContext = MainFunct.getCurrentContext();
I would try things in this order:
eyeGestureContext = getApplicationContext();
eyeGestureContext = getApplication();
If above does not work I would extend the Application and do:
public class MyExtendedApplication extends Application {
private static MyExtendedApplication instance;
public static MyExtendedApplication getInstance() {
return instance;
}
}
This works for me with global "android.net.conn.CONNECTIVITY_CHANGE" broadcast
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
so should also for you with "com.google.android.glass.action.EYE_GESTURE"
Watching adb logcat in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never hits the event bus; instead, it skips straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button. So it looks like the eye-gesture API was removed without announcement.
The receiver should extend the BroadcastReceiver class.
Define the receiver in the manifest
In the code (maybe onCreate), register the receiver
Create a receiver object
Define the intent filters
call RegisterReceiver() passing in the receiver and the intent filters

My Broadcast Receiver doesn't receive for intents

I'm developing an app and i need another external application with a broadcast receiver only. Here is my code:
app1:
Intent intent = new Intent();
intent.setAction("com.blabla.myaction");
intent.putExtra("extra", "test");
sendBroadcast(intent);
app2 (The one with the receiver):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver
android:name=".myReceiver">
<intent-filter>
<action android:name="com.blabla.myaction" />
</intent-filter>
</receiver>
</manifest>
public class myReceiver extends BroadcastReceiver {
private Context mContext;
public static final String ACTION = "com.blabla.myaction";
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (ACTION.equals(intent.getAction())) {
Log.e("lala", "received");
String extra = intent.getStringExtra("extra");
if (packageName != null) {
Log.e("lala", extra);
}
}
}
With this, i doesn't get the "received" log nor the extra. Why?
You "recieving application has to have been started at least once.
You may want to review https://developer.android.com/reference/android/content/BroadcastReceiver.html.

Give a Call to BroadCast Reciever Through An Activity

I am a newbie to Android development. I am trying to invent a broadcast receiver from my activity.
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"ABC", Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
String[] parameters= (String[])intent.getSerializableExtra("parameters");
}
}
my activity is
public class MyActivity extends Activity {
public static String BROADCAST_ACTION="com.kiosk.cbal.CALL_RECEIVER";
/**
* #see android.app.Activity#onCreate(Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String parameters = "safdsam,fdsa,fdsa,fdsa,fdsa";
String[] parameters =abc.split(",");
Intent i = new Intent("com.package.MyReceiver");
i.putExtra("parameters", parameters);
sendBroadcast(i);
}
}
My Manifest file is
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.package" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name="MyActivity">
<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="com.kiosk.cbal.CALL_RECEIVER"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
</manifest>
Now how I'll Be able to give a call to broadcast receiver through an activity?
Remove the space from the action name. Use the name when you create the Intent.
Update: The code above still has Intent i = new Intent("com.package.MyReceiver");. It should be Intent i = new Intent("com.kiosk.cbal.CALL_RECEIVER");
Meanwhile, in your manifest the receiver name is specified as <receiver android:name="MyReceiver">. note that android:name must be either a fully qualified class name, or a name relative to the package name. The way PackageManager distinguishes the two is by the presence of a . at the beginning of the name. Thus, with your declaration, PackageManager is most likely attempting to instantiate MyReceiver' instead ofcom.package.MyReceiver`.
In any case, you should check the Android log file for details on what's going wrong with the intent you're broadcasting.
The string that you pass to the Intent constructor must be the same as the string specified for the action element for your receiver in the manifest. You are constructing the Intent with your receiver class name, but your receiver is declared to listen to the VIEW intent instead.

Categories

Resources