How to detect android headset button click on 4.0 and higher - android

I am trying to detect Headset button click referring to this
http://android.amberfog.com/?p=415
This is the code I haave tried
public class MainActivity extends Activity {
private IntentFilter mIntentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actvity_main);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_MEDIA_BUTTON);
mIntentFilter.setPriority(2147483647);
registerReceiver(mReceiver, mIntentFilter);
}
#Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
Toast.makeText(MainActivity.this,"Your Message", Toast.LENGTH_LONG).show();
}
abortBroadcast();
}
};
#Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
All I am trying to do is get a toast message for the button click received , but it is just not getting detected What exactly should be done to detect the button click .
I am testing on a real android device with Android 4.2.1

Have you registered your broadcast receiver in AndroidManifest.xml?
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

Related

Detect Enable/Disable Bluetooth

I made an application to turn on/off bluetooth. To turn on/off i used a switch button. Here is the code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = null;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch (state){
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_ON:
break;
}
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null){
//Device does not support bluetooth
}
else{
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
}
});
}
}
}
The problem is that if i turn on/off bluetooth from settings and not from my app the switch doesn't change.
I have implemented a broadcast receiver but i can't access the switch from it.
I tried:
bluetoothSwich.setChecked(true)
inside the broadcast receiver but it doesn't work.
Ty
EDIT :
I partially solved the problem with the global switch but to catch the on/off action from settings first i have to click the on/off button from my app at least one time. any suggestion?
To detect the state change of bluetooth you need to add following permission to your AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH" />
Use a Local broadcast preferably. You do not need to register it in Manifest . register it at runtime where you need it.(If need throughout the app then register it in Manifest)
private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
// Bluetooth is disconnected, do handling here
}
}
}
};
Runtime register:
LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
Runtime unregister : Don't forget to unregister the broadcast.
LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);
Static Register:
<receiver
android:name=".FullyQualifiedBroadcastReceiverClassName"
android:enabled="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>

Broadcast receiver not working?

I m using a broadcast receiver with multiple intent filters. My requirement is to access the click of notification and make a job with that click in background(if app minimised) or foreground. The first two jobs are finishing, but the third one not executing. Where i m wrong ?
Here is my code
public class MainActivity extends Activity{
int choice; EditText et;
#override
onCreate(Bundle SavedInstanceState){
setContentView(R.layout.main);
et= (EditText)findViewById(R.id.choice);
choice=Integer.parseInt(et.getText().toString());
//some_stuff
if (choice==1)
registerReceiver(br, new IntentFilter("ACT_ONE");
else if (choice==2){
registerReceiver(br, new IntentFilter("ACT_TWO");
registerReceiver(br, new IntentFilter("ACT_THREE");
}
else if (choice==3) {
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
nb.setContentTitle("Do Job_Four");
nb.setContentText("Click here to do job four");
nb.setAutoCancel(true);
nb.setSmallIcon(R.drawable.icon);
nb.setDefaults(Notification.DEFAULT_ALL);
Intent in=new Intent(this,MainActivity.class);
IntentFilter lf=new IntentFilter("ACT_FOUR");
PendingIntent pi=PendingIntent.getBroadcast(this,(int)System.currentTimeMillis(),in,0);
nb.setContentIntent(pi);
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
registerReceiver(br,lf);
nm.notify((int)System.currentTimeMillis(),nb.build());
}
}
private void m_one(){
//job_one_code
}
private void m_two(){
//job_two_code
}
private void m_three(){
//job_three_code
}
BroadcastReceiver br=new BroadcastReceiver(){
#override
onReceive(Context c, Intent i){
if(i.getAction().equals("ACT_ONE")
m_one();
else if(i.getAction().equals("ACT_TWO")
m_two();
else if(i.getAction().equals("ACT_THREE")
m_two();
else if(i.getAction().equals("ACT_FOUR")
m_three();
}
};
#override
public void onDestroy(){
unregisterReceiver(br);
super.onDestroy();
}
}
Here is manifest
<receiver android:name=".MainActivity">
<intent-filter android:priority="999">
<action android:name="ACT_ONE"/>
</intent-filter>
</receiver>
my pseudo code:-- Please register onPause() and unregister
on onResume() method it will work.
public class AndroidBroadcast extends Activity {
private BroadcastReceiver the_receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context c, Intent i) {
}
};
// Set When broadcast event will fire.
private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction("ax.chayan.mybroadcast");
sendBroadcast(intent);
}
#Override
protected void onResume() {
// Register reciever if activity is in front
this.registerReceiver(the_receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// Unregister reciever if activity is not in front
this.unregisterReceiver(the_receiver);
super.onPause();
}
}

IS there any way to open our app in android when anyone unlocks the screen

i want to open my app every time when user unlocks it home screen.Please help me this app is only for my personal use so any help will be great
thanks
Register your application to receive the SCREEN_ON intent by registering a receiver in your manifest:
<receiver android:name=".receiverScreenUnlocked" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
</application>
Write the receiver class to receive an intent when user unloks the screen and run the activity you want:
public class receiverScreenUnlocked extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
*
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = true;
}
}
}
Example
public class ExampleActivity extends Activity {
#Override
protected void onCreate() {
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// YOUR CODE
}
#Override
protected void onPause() {
// WHEN THE SCREEN IS ABOUT TO TURN OFF
if (ScreenReceiver.wasScreenOn) {
// THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED OFF");
} else {
// THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onPause();
}
#Override
protected void onResume() {
// ONLY WHEN SCREEN TURNS ON
if (!ScreenReceiver.wasScreenOn) {
// THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED ON");
} else {
// THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onResume();
}
}
Reference

AIRPLANE_MODE action not receiving

android Airplane mode not showing logs
my code is as below
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(ACTION_AIRPLANE_MODE);
registerReceiver(mBroadcastReceiver, intentFilter);
// AppRater.app_launched(new WeakReference<Activity>(this));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver);
}
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Mass", "Action " + intent.getAction());
if (intent.getAction().equals(ACTION_AIRPLANE_MODE)) {
mHandlerRedrawUI.sendEmptyMessage(UPDATE_VISIBILITY);
}
}
};
private static final int UPDATE_VISIBILITY = 1;
private static final String ACTION_AIRPLANE_MODE = "android.intent.action.AIRPLANE_MODE";
Handler mHandlerRedrawUI = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == UPDATE_VISIBILITY) {
}
}
};
}
I just turned on Airplan mode and turned off not getting my log receicer
like
<receiver android:name="com.example.Bootreciever">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
Sometime we should think outside box :D :P
I was connected my device through wifi and testing, when I was enabling Airplane mode it was getting disconnected so I was not getting logs, The code is fine and its works as I needed

Listening for ACTION_DREAMING_STOPPED

How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories

Resources