Android BroadcastReceiver to get all SDCARD Status - android

I'm trying to get all SD card statuses using the BroadcastReceiver, but the code isn't working correctly. The BroadcastReceiver does not give a comprehensive log after a change in SD card status, such as Turn on(off) USB Storage. Any ideas?
Manifest receiver actions:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_EJECT"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
</intent-filter>
</receiver>
OR this receiver defined:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL"/>
<action android:name="android.intent.action.ACTION_MEDIA_EJECT"/>
<action android:name="android.intent.action.ACTION_MEDIA_REMOVED"/>
<action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTED"/>
</intent-filter>
</receiver>
BroadcastReceiver class to detect
public class BroadcastChangeSDCardStatus extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean status = getStorageStatus();
Log.e("SDCARD Status: ", status + "");
}
public static boolean getStorageStatus() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return false;
} else {
return false;
}
}
}

<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
</intent-filter>
Above code should work but as you said you already tried this so it looks like something is wrong with your receiver name .Your broadcast receivers is not registered.Give receiver name complete package name with class name correctly

You also need to set the android:scheme to "file" :
Do this by adding <data android:scheme="file" /> to your receiver like:
<receiver android:name=".Broadcasts.BroadcastChangeSDCardStatus">
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_EJECT"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
Hope this helps you out

i'm after adding this permission resolve my problem now:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

Related

i have a problem in my BroadcastReceiver, after the device is reboot receiver is not called

my program extends From BroadCastReciever to set Alarm, after the device is reboot receiver is not called, this is my code
1- BroadcastReceiver
public class myReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()!=null) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Log.d("myActivity","repooted");
// alarm settings
}
}
}
2-xml
<receiver
android:name=".sync.myReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
In your AndroidManifest.xml have you added permission to receive boot complete?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

OnReceive BroadcastReceiver not fired when outgoing call is answered

I want to detect the outgoing call when it is answered, I use a BroadcastReciever with the following code:
class OutgoingReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string number = intent.GetStringExtra(Intent.ExtraPhoneNumber);
string stateStr = intent.Extras.GetString(TelephonyManager.ExtraState);
if (stateStr!=null)
{
if (stateStr.Equals(TelephonyManager.ExtraStateOffhook))
{ }
else if (stateStr.Equals(TelephonyManager.ExtraStateIdle))
{ }
}
}
}
and use with this code the following permissions:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE" />
And also with this receiver on AndroidManifest.xml
<receiver android:name="OutgoingBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE_CHANGED" />
</intent-filter>
</receiver>
But it is fired only when the outgoing call is start or end.
Please, Help me!!

use Broadcast Receiver to catch incoming call, onReceive not triggered? Log cannot be printed out

In broadcastReceiver:
public void onReceive(final Context context, Intent intent) {
Log.e("Current:","entered111!");
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Intent intent1=new Intent(context,myService.class);
intent1.putExtra("incomingNumber",incomingNumber);
context.startService(intent1);
}
}
This is my Manifest file
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".myService" />
<receiver android:name=".ServiceReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
You have to add below permission in your manifest file;
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Then enable 'Phone' in 'Permissions' of your application in 'Settings' -> 'Apps' -> Application -> 'Permissions'.
Thank you.

Direct share is not working with my app. ChooserTargetService is not called

I referred sample application code (https://github.com/googlesamples/android-DirectShare) and I added ChooserTargetService to my application
public class ShareChooserTargetService extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
In Manifest file I added these lines:
<activity
android:name=".ui.ShareActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService"
android:label="#string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
But ShareChooserTargetService is not called, log isn't printed. I run sample application in my phone and it works perfectly but these code in my app isn't working. I am unable to find what is the issue. Can anyone help please?

Android, When user installs an app?

I want to know when users installs a new app. I have used broadcast receiver.
<receiver android:name=".NewInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.package_added" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Code:
public class NewInstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String act = intent.getAction();
Log.e("ee", "new install" + intent);
if (Intent.ACTION_PACKAGE_ADDED.equals(act) || Intent.ACTION_PACKAGE_REMOVED.equals(act)) {
/* ContentResolver contentResolver = context.getContentResolver();
contentResolver.query()*/
}
}
}
I see no logs when i install app from playstore.
Should I use service? If yes then how can i make it run for infinite times?
I guess there is typo in your intent filter, try below intent filter
<receiver android:name=".NewInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package" />
</intent-filter>
</receiver>

Categories

Resources