How to detect bluetooth call/media button press in android app - android

I need to detect the bluetooth device button click in my application. i followed many stackoverflow links, but doesn't seem to work for me.
I am using the broadcast receiver as shown below:
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
//call my method
}
}
}
}
and my manifest is as follows:
<receiver android:name=".RemoteControlReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
can any one suggest a way out?
Thanks in advance.

Is your API level at least 11? The code KEYCODE_MEDIA_PLAY was added in API level 11. The KEYCODE_MEDIA_PLAY_PAUSE code exists since API level 3.
Also, have you tried to configure your intent filter without specifying a category?
Is your RemoteControlReceiver class in the root package of your application? It might have not been able to find ".RemoteControlReceiver".
Other than that, I can't see where you could be doing anything wrong.
I've read in a few posts that you might have to also call registerMediaButtonEventReceiver and unregisterMediaButtonEventReceiver. Have you tried this?
To register:
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
componentName = new ComponentName(getPackageName(),
RemoteControlReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(componentName);
And to unregister:
audioManager.unregisterMediaButtonEventReceiver(componentName);

Related

Android Smart Button in headphone connector

I bought from local eshop device called "Android Smart Button". It is similar to Pressy, but I didnĀ“t know that it is from Chine including application. When I tried to download app, Google Chrome warned me that it is not safe. So I would like to program own application to control it, but I do not know, when I can receive button click. Can you advice me? Thank you.
I tried this, but it did not work.
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: ");
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int keyCode = event.getKeyCode();
}
Today I found solution provided by Fouad Wahabi and it also works for this Smart Button from Chine. So now I can finally replace very untrusted application called KlicK.

Android Hidden App start from dialer

We can hide the android app from launcher by editing manifest XML,but is there any code snippet or example by which we can hide the app and start it by entering some code like ##4444## like that.Any way to do this??
Thanks in advance.
To start your app from dialer you need to do three things:
1. Add receiver to your AdroidManifest.xml
<receiver android:name="com.example.HiddedReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
2. Create BroadcastReceiver as stated in xml. It will intercept EVERY calls number. You just need to scan it for your string and do apropiate action - in this case fire off an intent.
public class HiddenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
String resultData = getResultData();
if (resultData != null) {
if (resultData.contains("YOURCODE")) {
setResultData(null); // it wont continue calling that number
//HERE CREATE your intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
}
}
3. To get this working, you need to tell android you will be using this feature, and to grant permission to process calls from the user at install time.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
i didnt test it, but this one works like a charm:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Android lock screen : activity started first when mobile on

I develop one apps mobile number lock, i want whenever mobile on,or restart or switch on ,or on from the top/left/right button placed on mobile in short whenever mobile screen on my lock activity call, i have no idea to how to call activity at time of mobile on please any one give some related example to start activity at first when mobile on. so my lock dispay to user and then enter number password and lock open ...thanks in advance..
Following is working for me :
enter code here :
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context,ViewPagerMainActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}
and in menifist file add follwing:
enter code here :
<receiver android:name=".BootReciever">
<intent-filter android:enabled="true" android:exported="true">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
There are multiple ways, to achieve this. One way is you can go through Broadcasting a message for your app, after the boot of your mobile. Try reading:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
Also have look at this thread, this will solve your problem.
How to launch activity on BroadcastReceiver when boot complete on Android

Android re-enable a disabled App upon installation of update

Im currently disabling an application via
setApplicationEnabledSetting(String, int, int)
The application is actually disabling itself.
I would expect upon re-installation of the app, the app would re-enable itself, however this isnt the case.
Is there a particular setting required in the manifest to make this work.
(Ive tried setting enabled=true)
Thanks
Im currently disabling all components apart from a broadcast receiver and am trying to catch the installation process to re-enable all the other components again. Which is nasty to say the least
One way to do this is to listen to package installation broadcasts and take action accordingly.
Listen to Intent.ACTION_PACKAGE_ADDED in your broadcast receiver.
If the newly added package is yours, enable the other components.
Example
Manifest:
<receiver android:name =".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
Receiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
final String packageName = intent.getData().getSchemeSpecificPart();
if (replacing && "my.package.name".equals(packageName)) {
// Re-enable the other components
}
}
}
}
Hope this helps.

How do I access the user calls to mobile?

I want my application to be activated when the user make a specific call. Is there any way to take information which call is making by user in the same time ( not afterwords ) in order to activate the app at the right time ?
Ok i wrote this code for my case and it works:
public class OutgoingCallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if( phonenumber.equals("11111111") ) {
Intent myactivity = new Intent(context, MyKeyboard.class);
myactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myactivity);
}
}
}
In the Manifest i add this:
<receiver
android:name=".OutgoingCallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
I don't really know, but I think that's a really dangerous practice. It all depends on what you intend to do with your app. If it's a recording app, I think you can't even think of developping it, or don't post on the internet about it because it would be illegal in many countries. That's how I see and how I feel your question. About Android there is not 15k places to search, have a look at the android API.
http://developer.android.com/reference/android/provider/CallLog.Calls.html
This, for example, I don't know if it's a real time log, or if it's filled after the call is terminated. But you can search in that direction.

Categories

Resources