BroadcastReceiver is not able to receive the Intent - android

I want to write a BroadcastReceiver to receive the application install action. But it failed, so I test if my receiver is well or not. So custom a intent, it also filed. below is my code. Please help me correct it.
public class MyInstallReceiver extends BroadcastReceiver {
// public MyInstallReceiver() {
// }
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Log.d("receiver", "Intent Detected");
if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString ();
//System.out.println ("installed:" + packageName + "package name of the program");
Log.d("receiver","installed:" + packageName + "package name of the program");
}
}
}
custom intent
public void installAPK(View v){
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
Log.d("receiver", "Intent sent");
}
Manifest.xml
<receiver
android:name=".MyInstallReceiver"
android:enabled="true"
android:exported="true" >
<Intent-filter>
<action android:name = "android.intent.action.PACKAGE_ADDED"/>
<action android:name = "android.intent.action.PACKAGE_REMOVED"/>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
<Data android:scheme = "package" />
</Intent-filter>
</receiver>
enter code here

I don't know about correct spelling in your manifest, but this code is definitely works very well:
<receiver android:name=".MyInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Every application install/uninstall will trigger this receiver.

Everythong looks good, expect a typo in your manifest. It should be <intent-filter> and not <Intent-filter>

Related

ACTION_PACKAGE_ADDED not working

I have to log when the user installs my app, for that I have registered ACTION_PACKAGE_ADDED broadcast in manifest and created a receiver. but the receiver is not triggering after installing the app. I have tried with registering broadcast ACTION_PACKAGE_ADDED instead of PACKAGE_ADDED, and it works. I have read most of the SO questions regarding this and tried, but nothing works for me.I giving my code below
Manifest
<receiver android:name=".AppInstallReceiver">
<intent-filter android:priority="1">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Recevier
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString();
Log.i("Installed:", packageName + "package name of the program");
}
Log.d(TAG, "Action: " + intent.getAction());
Uri data = intent.getData();
Log.d(TAG, "The DATA: " + data);
}
Is there any problem with this code ?

Open Activity With Dial Code

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"/>

BroadcastReceiver not called

I found lots of threads about this topic, however I'm not able to solve my problem. Here is the code:
The manifest file:
<service
android:name="com.xxx.player.MediaPlayerService.MediaPlayerService"
android:enabled="true" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<receiver android:name="com.xxx.player.MediaPlayerService.MediaPlayerService$ServiceBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="#string/ACTION_PREPARE_AND_PLAY_NEW_FILE"/>
<action android:name="#string/ACTION_PREPARE_NEW_FILE"/>
<action android:name="#string/ACTION_START"/>
<action android:name="#string/ACTION_STOP"/>
<action android:name="#string/ACTION_PAUSE"/>
<action android:name="#string/ACTION_SEEK_TO"/>
<action android:name="#string/ACTION_RELEASE"/>
</intent-filter>
</receiver>
</service>
The broadcast class:
public class MediaPlayerService extends Service implements
MediaPlayer.OnErrorListener,
AudioManager.OnAudioFocusChangeListener,
Runnable,
SeekBar.OnSeekBarChangeListener {
...
public class ServiceBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "action: " + action, 30000000).show();
if (action.equals(Resources.getSystem().getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE))) {
prepareAndPlayNewFile(intent.getStringExtra("mediaData"));
}
}
}
}
The way I submit the intent:
private void prepareAndPlayNewFile(String mediaData) {
Intent myIntent = new Intent();
myIntent.setAction(context.getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE));
myIntent.putExtra("mediaData", mediaData);
context.sendBroadcast(myIntent);
}
Instead of approaching your playing of media this way, you should instead bind your Service to your Activity instead of using a broadcast receiver for message passing.
Also you shouldn't be using #string/VAR1 (which I'm not sure if intent filters work with that kind of string definition) for your intent actions it ALWAYS should be the constant string such as:
android.intent.action.BLAH

get number, which customer dialed from standard dialpad

i try catch a call from standard dialpad using that code:
<action android:name="android.intent.action.CALL" />
<data android:scheme="tel" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CALL_PRIVILEGED" />
</intent-filter>
</activity>
Everything fine, when user dial from standard phone dialpad, my app opened.
But i don't find solution, how i can get a phone number, which user was dialed. That code inside PhonePadActivity activity onCreate block:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
gives me a null finally :(
tried to using brodacast receiver:
in manifest:
<receiver
android:exported="true"
android:name="com.myapps.android.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
this is class DialBroadcastReceiver:
package com.myapps.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DialBroadcastReceiver extends BroadcastReceiver {
private static final String THIS_FILE = "PhonePadActivity";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e(THIS_FILE,"In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e(THIS_FILE,"Number is: "+number);
}
}
}
but logs nod fired, when user press dial
This works for me:
In Manifest:
<intent-filter>
<action android:name="android.intent.action.CALL"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.CALL_PRIVILEGED"/>
<data android:scheme="tel"/>
</intent-filter>
In Activity:
String inputURI = this.getIntent().getDataString();
if (inputURI != null) {
Uri uri = Uri.parse(Uri.decode(inputURI));
if (uri.getScheme().equals("tel")) {
String calledNumber = uri.toString();
}
}
You are getting the number with incorrect code. Replace:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
With:
Uri data = getIntent().getData();
if (data != null && ("tel".equals(data.getScheme()))) {
String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
if (number != null) {
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
}
}
Add <intent-filter android:priority="9999"> to the intent-filter declaration in the manifest, to make sure you're first in line
Remove the com.myapps.android-part from the android:name property of the receiver declaration in the manifest (i.e. it should be: android:name=".DialBroadcastReceiver")
Register a BroadcastReceiver like this in Manifest file:
<!-- DIAL Receiver -->
<receiver
android:exported="true"
android:name="receivers.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
You need Permission for NEW_OUTGOING_CALL :
Eventually you can Receive the broadcast and retrieve the dialed number like this:
public class DialBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("DileBroadCastReceiver","In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.v("DialBroadcast Receiver","Number is: "+number);
}
}
}

Android BroadcastReceiver can't receive any broadcast events

I have created a BroadcastReceiver to detect SDCard mount and unmount
event, however, I am not able to receive any events at all:
here's the AndroidManifest.xml:
<receiver android:enabled="true" android:label="SDCardMountReceiver" android:exported="true" android:name="xxx.broadcasts.SDCardBroadcastReceiver">
<intent-filter>
<action android:name="android.content.Intent.ACTION_MEDIA_MOUNTED"></action>
<!-- or <action android:name="android.content.Intent.ACTION_MEDIA_UNMOUNTED" />--></intent-filter>
</receiver>
And the SDCardMountReceiver class:
public class SDCardBroadcastReceiver extends BroadcastReceiver {
public SDCardBroadcastReceiver() {
super();
System.err.println("constructor");
}
public void onReceive(Context context, Intent intent) {
Log.d("SDCardBroadCastReceiver", "receive " + intent.getAction());
System.err.println("jonathan receive " + intent.getAction());
}
}
You also need to set the data scheme to "file".
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
Reference: android-developers thread
The Intent javadoc specifies a different action:name value.
Use "android.intent.action.MEDIA_MOUNTED" instead of "android.content.Intent.ACTION_MEDIA_MOUNTED"
If you register a broadcast receiver programmatically, you must also set the scheme to "file".
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
mContext.registerReceiver(mExternalStorageReceiver, filter);

Categories

Resources