BroadcastReceiver when there is no LAUNCHER, on Kitkat

I have a project in which I have to create an application with the following properties:

  • No LAUNCHER : category android: name = "android.intent.category.LAUNCHER"
  • He should read SMS messages sent from a specific number, and when this happens, start Activity
  • Work with most versions of Android (my goal is now from [Froyo, 2.2] to [Kitkat, 4.4] )

So far my problem is that in Kitkat my BoradcastReceiver does not work when the application is just installed, however, if the application starts once, it runs correctly. In previous versions, it behaves correctly. I read that in the version of [HoneyComb 3.1] there have been changes in the broadcast system, for example this question shows that my problem is known.

My question is: is there a way to install APP and not shut up until it is needed. For example, how do they work on versions below [Honeycomb]?

  • If so, can someone indicate the direction.
  • If not, would I launch the application once at boot time, and then close it would be a reasonable approach?
  • In any case: what do I create β€œfeels” wrong, since what I do is considered bad practice? My system requires the user to actively install the APP, and the APP aims to initiate additional sounds / movement during the message to indicate that the user is at physical risk. The system is used to suggest that the user, his house or part of his / her property is in direct danger, so he should wake up and notify him / her in most cases.

MANIFESTO:

<?xml version="1.0" encoding="utf-8"?> 

 <!--<uses-sdk android:minSdkVersion="8" /> --> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_iconedesktop" android:label="@string/app_name" android:theme="@style/AppTheme"> <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> <receiver android:name=".SMSbroadcastReceptor"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> 

RECEIVER

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; public class SMSbroadcastReceptor extends BroadcastReceiver { final SmsManager sms = SmsManager.getDefault(); public void onReceive(Context contexto, Intent intencao) { final Bundle bundle = intencao.getExtras(); try { if (bundle != null) { final Object[] pdusObj = (Object[]) bundle.get("pdus"); String quem = ""; String mensagem = ""; for (int i = 0; i < pdusObj.length; i++) { SmsMessage smsRecebido = SmsMessage.createFromPdu((byte[]) pdusObj[i]); quem = smsRecebido.getDisplayOriginatingAddress(); mensagem = smsRecebido.getDisplayMessageBody(); Log.i("SMSbroadcastReceptor", "Quem: " + quem + "\n, O que: " + mensagem); Toast toast = Toast.makeText(contexto, "Quem: " + quem + "\n, O que: " + mensagem, Toast.LENGTH_LONG); toast.show(); } if (quem.equals("+MY HIDDEN NUMBER IS HERE")) {// // abortBroadcast(); Intent comecarAMain = new Intent(contexto, MainActivity.class); comecarAMain.putExtra("MY PACKAGE", "A COMMAND"); comecarAMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contexto.startActivity(comecarAMain); } } } catch (Exception e) { Log.e("SMSbroadcastReceptor", "Excecao SMSbroadcastReceptor" + e); } } 

}

+1
source share
1 answer

Is there any way to install APP and keep it silent until needed

Not really. Something must use an explicit Intent to start one of the components of your application before any receivers registered with the manifest will work. If you don’t have a start screen, I don’t know what else would launch one of your application components using an explicit Intent .

would launch the application once at boot time and then close it would be a smart approach?

No, because you have no way to gain control at boot time, except through the registered BroadcastReceiver manifest, which puts you in the same position as you.

My system requires the user to actively install APP

Then there should be no particular problems with launch activation, at least for a single launch. You can disable this activity, but if the user forcibly stops your application, you will again be in a stopped state and will no longer respond to broadcasts.

and the APP aims to initiate additional sounds / movement when reporting to indicate that the user is in physical danger

Then there should be no problem with a simple exit from the launch. In the end, the user must be able to customize the behavior of your application.

+2
source

Source: https://habr.com/ru/post/1487530/


All Articles