Do I need to launch a compareToIgnoreCase? - android

I register a receiver using Code A in AndroidManifest.xml.
The Code B will handle something after mobile phone restart.
I think I can move the code if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) just like Code C.
I think that public void onReceive(Context context, Intent intent) will be launched only after mobile phone restart because I have set <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" />
Right?
Code A
<receiver android:name="bll.CleanupBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Code B
public class CleanupBootReceiver extends BroadcastReceiver{
private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
//To Do...
}
}
}
Code C
public class CleanupBootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//To Do...
}
}

I think I can move the code if (intent != null && intent.getAction()
!= null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) just
like Code C.
Yes you can, because you are using your receiver for only one intent-filter action.
But your broadcast receiver can still be triggered using explicit events like
sendBroadcast(new Intent(context, CleanupBootReceiver.class)
so it is good idea to keep with conditional match though I recommend you to use equalsIgnoreCase instead of compareToIgnoreCase
I think that public void onReceive(Context context, Intent intent)
will be launched only after mobile phone restart because I have set
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" />
Yes though a must read : Background Execution Limits

Related

Android how to start Activity when ACTION_SCREEN_ON and App is terminated

I want my app able to start an Activity whenever the app is terminated or running. but my code seems not work
<receiver
android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
Log.e("BAM","ACTION_SCREEN_ON");
Intent i = new Intent(context, SecondMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
should I call the BootReceiver in the OnCreate activity?

Launcher Activity running before Broadcast receiver

Houston I have a problem, here it goes:
I have a broadcast receiver that runs when my app is updated, I've achieved that using this:
Android Manifest, inside <application> tag:
<receiver
android:name=".control.background.services.UpdateReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package" android:path="br.com.soutsapp.waiter.soutsw" />
</intent-filter>
</receiver>
And this BroadcastReceiver class:
public class UpdateReceiver extends BroadcastReceiver {
public UpdateReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
String packageName = context.getPackageName();
if(packageName.equalsIgnoreCase("br.com.soutsapp.Souts_v3")){
final Context appContext = context.getApplicationContext();
SharedPreferences sp = appContext.getSharedPreferences(ConstantUtils.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String json = sp.getString(ConstantUtils.TAG_SHARED_PREFERENCES_USER_INFO, null);
UserModel user = null;
if(json != null){
user = new JsonParserUtils<>(UserModel.class).serialize(json);
}
if(user != null && user.getFacebookUserId() != null && user.getFacebookUserId().length() > 0){
FacebookSdk.sdkInitialize(appContext);
LoginManager.getInstance().logOut();
}
sp.edit().clear().commit();
Log.d("SOUTS", "Running broadcast receiver");
}
}
}
}
Inside my Launcher activity I've printed a log when onCreate ends;
And I got this two prints, when my app runs with a update:
06-28 21:58:48.629 24984-24984/br.com.soutsapp.Souts_v3 D/SOUTS:
Running onCreate EstablishmentActivity
06-28 21:58:51.572 24984-24984/br.com.soutsapp.Souts_v3 D/SOUTS:
Running broadcast receiver
As you can see, broadcast receiver runs 3 seconds after the Launcher activity, so here goes my question, is there a way to be sure that my BroadcastReceiver will run before my Launcher activity?
If not, how would you suggest I workaround this?

How do I listen for a package to install before registering a ContentObserver?

My issue here is more the result of the framework I'm using than Android itself. My app needs to register a ContentObserver after the user has installed a specific plugin associated with the AWARE framework. The framework has a sensor for installation of plugins, and sends a broadcast once this happens. Documentation can be found here: http://www.awareframework.com/installations/
I would like to accomplish this using a BroadcastReceiver.
I have a few questions regarding how to properly do this:
The way I interpret this documentation and Android is that if I register a BroadcastReceiver in my Manifest file with the intent filter "Installations.EXTRA_PACKAGE_NAME", every time a package is installed, it will activate my InstallationReceiver. If my understanding is still correct, I will be able to use getAction() on the received intent to parse the package name. Does this look like a correct understanding? Do intents works this way?
Is it proper to have my BrodcastReceiver in my Service class? Or should I make it a standalone class?
Did I register my receiver properly?
My MainService code:
package hcii.tracker;
public class MainService extends Service {
public static final Uri ACTIVITY_URI = Uri.parse("content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
public HashMap<Uri,ContentObserver> mContentObservers;
public void onCreate(){
Log.d("SERVICE", "Service created!");
Context context = this;
mContentObservers = new HashMap<Uri,ContentObserver>();
//Activate installations sensor
Aware.setSetting(context, Aware_Preferences.STATUS_INSTALLATIONS, true);
//Activate Accelerometer
Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
//Set sampling frequency
Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 60);
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 60,
"com.aware.plugin.google.fused_location");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.fused_location");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
try {
ContentObserver observer = mContentObservers.get(ACTIVITY_URI);
getContentResolver().unregisterContentObserver(observer);
mContentObservers.remove(ACTIVITY_URI);
} catch (IllegalStateException ise) {
Log.d("SERVICE", "No ContentObservers registered");
}
}
public class ActivityRecognitionObserver extends ContentObserver {
public Uri CONTENT_URI = Uri.parse("content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
public ActivityRecognitionObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// Get the latest recorded value
Log.d("OBSERVER", "Change in activity data detected");
Cursor activity = getContentResolver().query(CONTENT_URI, null, null, null,
"activity_name" + "DESC LIMIT 1");
if( activity != null && activity.moveToFirst() ) {
// Here we read the value
String activity_name = activity.getString(activity.getColumnIndex("activity_name"));
if (activity_name.equals("in_vehicle")){
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 60,
"com.aware.plugin.google.activity_recognition");
Log.d("OBSERVER", "Recognized in vehicle");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
}
else {
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 180,
"com.aware.plugin.google.activity_recognition");
Log.d("OBSERVER", "Recognized on foot");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
}
}
if( activity != null && ! activity.isClosed() ) activity.close();
}
}
public class InstallationReceiver extends BroadcastReceiver {
public InstallationReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals("com.aware.plugin.google.activity_recognition")){
ActivityRecognitionObserver so = new ActivityRecognitionObserver(new Handler());
getContentResolver().registerContentObserver(ACTIVITY_URI, true, so);
mContentObservers.put(ACTIVITY_URI, so);
Log.d("SERVICE", "Observer registered");
}
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="hcii.tracker" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
tools:replace="android:icon, android:theme" >
<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>
<service
android:name=".MainService"
android:exported="false" />
<receiver android:name="hcii.tracker.MainService$InstallationReceiver" >
<intent-filter>
<action android:name="Installations.EXTRA_PACKAGE_NAME" >
</action>
</intent-filter>
</receiver>
...
There are several problems here:
1) You can use an inner class for your BroadcastReceiver, but if so it must be declared static, because Android needs to be able to instantiate the class itself using new. This is not possible in your current architecture (because the class is an inner class). In general it is better (easier) to use a stand-alone class for this.
2) Your Intent filter is wrong. The linked documentation from Aware indicates that the broadcast Intent is Installations.ACTION_AWARE_APPLICATION_ADDED, so your intent filter needs to look like this:
<intent-filter>
<action android:name="Installations.ACTION_AWARE_APPLICATION_ADDED"/>
</intent-filter>
3) Once you have declared the BroadcastReceiver correctly, it will be triggered whenever Aware adds a plugin. The package name of the installed plugin is put into an "extra" in the Intent. To determine which plugin has been installed, you need to do something like this:
public void onReceive(Context context, Intent intent) {
String packageName = intent.getStringExtra("Installations.EXTRA_PACKAGE_NAME");
if (packageName != null && packageName.equals("your.plugin.package.name") {
// Do your stuff here...
}
}

Get confirmation call answered on android

I'm doing a handle on android phone numbers, you can know if a call is made​​, ie confirm that the call was answered. Use the following code to make the call:
Intent callIntent = new Intent (Intent.ACTION_CALL, Uri.parse (number));
startActivity (callIntent);
When you end the call back to my application and that's when I want to know if the call was answered and if it could be the duration of the call.
Yes you can, for this you need create BroadCastReceiver with intent
for example
<receiver android:name=".broadcast.DontMissReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and then on Receiver
#Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(PHONE_ACTION)) {
this.receivePhoneCall(context, intent);
}
}
private void receivePhoneCall(Context context, Intent intent) {
String curState = intent.getExtras().getString("state");
if (curState.equalsIgnoreCase("RINGING")) {
} else
if (curState.equalsIgnoreCase("IDLE") && state.length() > 0) {
if (!state.equalsIgnoreCase("OFFHOOK")) {
}
}
}

Not getting the SD Card related intents to my broadcast receiver

I am trying to register a receiver for the removal of the sdcard, but my receiver is not getting called on removal of the sd card pasting my code here. I am registering the receiver in the oncreate() and unregistering in the ondestroy function. Please let me know if i am doing any mistake.
void registerSDCardStateChangeListener() {
final String MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
final String MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
final String MEDIA_BAD_REMOVAL = "android.intent.action.MEDIA_BAD_REMOVAL";
// final String MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
final String MEDIA_EJECT = "android.intent.action.MEDIA_SCANNER_FINISHED";
mSDCardStateChangeListener = new BroadcastReceiver() {
#
Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(MEDIA_REMOVED) || action.equalsIgnoreCase(MEDIA_UNMOUNTED) || action.equalsIgnoreCase(MEDIA_BAD_REMOVAL) || action.equalsIgnoreCase(MEDIA_EJECT)) {
if (mMediaPlayer != null) {
stopPlayBack();
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(MEDIA_REMOVED);
filter.addAction(MEDIA_UNMOUNTED);
filter.addAction(MEDIA_BAD_REMOVAL);
filter.addAction(MEDIA_EJECT);
registerReceiver(mSDCardStateChangeListener, filter);
}
Please let me know if anything is wrong in my code.
Try adding this to your intent-filter
filter.addDataScheme("file");
It appears the actions you are trying to catch send the path as the data field of the intent. If that is the case, then your intent filter must have the matching scheme.
Finally, and this is just a suggestion, but I would recommend you use the constants in the Intent class instead of typing out your actions manually. IE, use Intent.ACTION_MEDIA_REMOVED instead of you using the string directly.
Justin Breitfeller's answer worked for me, but as an alternate technique, you can also do this entirely in the AndroidManifest.xml:
<receiver android:enabled="true" android:exported="false"
android:name="SDCardStateChangeListener">
<intent-filter>
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
<data android:scheme="file" />
</intent-filter>
</receiver>
Then make the receiver a proper class:
public class OnMediaMountedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED)
|| action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED)
|| action.equalsIgnoreCase(Intent.ACTION_MEDIA_BAD_REMOVAL)
|| action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) {
if(mMediaPlayer != null) {
stopPlayBack();
}
}
}
};
I have found solutions while app is running and user unmount/removed sdcard.
#Override
protected void onResume() {
super.onStart();
IntentFilter ejectFilter = new ntentFilter(Intent.ACTION_MEDIA_MOUNTED);
ejectFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
ejectFilter.addAction(Intent.ACTION_MEDIA_EJECT);
ejectFilter.addDataScheme("file");
registerReceiver(ejectReceiver, ejectFilter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(ejectReceiver);
}
In class, define method like below,
BroadcastReceiver ejectReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_EJECT) || action.equals(Intent.ACTION_MEDIA_UNMOUNTED))
{
Log.e("msg","Media Unmounted");
//do something on unmounted sdcard from device
}
}
};
Hope it will be helpful.

Categories

Resources