How to launch an android service from a custom call number - android

I'm developing an Android application, i have a service running in background that i want to lauch it by introducing a custom ussd number. For example when i call #12345# my service starts.Thank you in advance

You can register a BroadcastReceiver in your manifest that listens for android.provider.Telephony.SECRET_CODE intents. You also specify the secret code in the manifest.
For example, this manifest entry registers MyBroadcastReceiver for the secret code *#*#12345#*#*.
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="12345"/>
</intent-filter>
</receiver>
MyBroadcastReceiver should look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Create an intent to start your Service.
}
}

Related

Broadcast Receiver is not working properly

I am experimenting with broadcast receiver in android and I have this class as broadcast receiver:
public class BroadcastInbuilt extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Battery Low, Please Charge!", Toast.LENGTH_LONG).show();
Log.i("BroadcastPersonal","Happening");
MainActivity.b1.setText("Change text if working!!");
}
}
I have added this in Android Manifest inside application tag:
<receiver
android:name=".BroadcastInbuilt"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
There is no code in my main activity. When I change my battery level to low the toast does not appear. i am following the android tutorial and I have done exactly same as told here
https://www.youtube.com/watch?v=Uf4V5OSJji8&list=PLlyCyjh2pUe9wv-hU4my-Nen_SvXIzxGB&index=44
Result is at 9:40 in video. Now his app shows toast but mine does not.
I did some research and found out that you should not register your receiver in Android Manifest but register them dynamically through programming.
BroadcastReceiver myBroadcast = new BroadcastInbuilt(); // name of class which extends BroadcastReceiver
IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_LOW);
this.registerReceiver(myBroadcast, filter);
Be sure to unregister broadcast in onDestroy()
unregisterReceiver(myBroadcast);
Note: if receiver was registered in onCreate() then it should be unregistered in onDestroy().If it was registered in onResume() then it should be unregistered in onPause().

Is it possible to send intents to another application like a command, telling it what to do?

I have an application that runs as a service in the background. I want another third-party application to be able to call multiple functionalities on this app via an intent. How would I achieve this. Presently the only thing i know how to do with intent is launching other applications and starting activities.
This can be achieved with a BroadcastReceiver.
Create a class that extends BroadcastReceiver and implements it's onReceive() method to handle intent appropriately:
#Override public void onReceive(Context context, Intent intent) {
if (ACTION.equals(intent.getAction())) {
// Do something..
}
}
This receiver must be declared in the manifest of the app and be exported and enabled like below:
<receiver android:name=".BroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="ACTION" />
</intent-filter>
</receiver>
You will need to send your intent from the first app as a broadcast:
sendBroadcast(intent);

BroadcastReceiver doesn't work

I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));

How to determine Device startup event in android

I want to mantain a log in my android application , log will contain the Device Started (Bootup) and Device Stop Times. Any Idea how to do this ?
I have to start my application on Bootup , But how to determine that application is started on Bootup ?
I have searched but could not find a better solution.
Use BroadCastReceiver to receive BOOT_COMPLETED broadcast. This broadcast is thrown in device startup
The receiver will be like
<receiver
android:name="ReceiverName"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You will need to use the following persmission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
now in code write a BroadCastReceiver class like
public class ReceiverName extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do startup tasks or start your luncher activity
}
}
You can use BroadcastReceiver component for this purpose. Using this you can detect various events of your device like booting.
To Detect Booting process you need to give permission in AndroidManifest.xml as below,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Then you need to create a BrodacastReceiver which will handle this,
In the onReceive() method the corresponding BroadcastReceiver would then start the event,
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WordService.class);
context.startService(service);
}
}

How to start an Application at boot up in Android?

I am trying to make an App in Titanium which launches on Startup i.e. as the mobile device startsup. I have seen code written at several places which states to do entry into the andsoid manifest file and some code like
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourActivity.class);
context.startActivity(myIntent);
}
But i am not able to figure out that where to put this code. In which file ?? and where ?
This 2 answers will do what you need:
Start BroadcastReceiver after some system broadcast:
https://stackoverflow.com/a/7877466/988434
Start BroadcastReceiver on boot:
https://stackoverflow.com/a/8544151/988434
in you BroadcastReceiver you'll implement just call whatever Service/Activty you need.
There an example for that in the question for the 2 answers above.
Tell me if you have any problems unanswered after reading those =].
You have to listen to the BOOT_COMPLETED intent filter. The piece of code you just quoted is from a BroadcastReceiver which will be firing up when the device will boot.
This class has to extend from BroadcastReceiver:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
...
}
}
Then, you have to register that receiver in your manifest file by doing the following:
<receiver
android:enabled="true"
android:name="your_package.BootReceiverClassName"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Also you need the following permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
By the way you have to make sure that the app is not installed on the SD Card otherwise it won't work (but there are possible workarounds).

Categories

Resources