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.
Related
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
I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
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.
I am new in android app development, i am making an answering machine app, and need help..
How to start a class which extends broadcastreceiver when i am in my launcher class ?
Which method to use for automatically picking up the call ?
Kindly help
Creating broadcast reciever
public class Yourclass extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
2.Registering broadcast reciever
You can register this either in manifest or by writing Context.registerReceiver() method.
Here is the example for manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
When your application is in defined state calls onRecieve() method to perform your required action.
I declared a BroadcsastReceiver(Action_headset_plug) in AndroidManifest.xml and defined a BroadcastHandler.class implement BroadcsastReceiver . I run the apk on the device and the receiver doesn't fire. However , it work correctly when I use registerReceiver() in the Activity. Do I miss something in the AndroidManifest.xml?
This is the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.Earphone_test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:enabled="true" android:name="BroadcastHandler">
<intent-filter>
<action android:name="android.intent.ACTION_HEADSET_PLUG"></action>
</intent-filter>
</receiver>
<activity android:name=".Earphone_testActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is the receiver code
public class BroadcastHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)){
String mes;
int state = intent.getIntExtra("state", 4);
if(state == 0){
mes ="out";
}else if(state == 1){
mes ="in";
}else {
mes ="others";
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Headset broadcast");
builder.setMessage(mes);
builder.setPositiveButton("Okey-dokey", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
}
For listening to headset changes, the broadcast receiver cannot be declared in the manifest, it must be dynamically registered. Not all receivers work when declared in the manifest and this is an example where you need to register it programmatically.
You can call this for example in an Activity's onResume method or in a Service's onCreate method:
headsetReceiver = new BroadcastHandler();
registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
Then in the Activity's onPause method or in the Service's onDestroy method you need to unregister the receiver:
unregisterReceiver(headsetReceiver);
The name is wrong in the manifest entry. Use the full package name, or start it with a period if you want it implicitly appended to the app's package name:
<receiver android:enabled="true" android:name=".BroadcastHandler">
Most examples of receivers do not start an AlertDialog but Toast a message or create a Notification in Status bar. I am sure that you cannot start an Activity because the "content" object stops existing but not if you can built an AlertDialog. (documentation of Broadcasts)
So you can try at your receiver a Toast message to make sure that it works.
Example with Notification: http://www.anddev.org/recognize-react_on_incoming_sms-t295.html