I am working on a Android Application project in which I am doing continuous task and when user click anywhere on home screen of phone I have to stop this work. Please anyone guide me how I can get touch event in my app so I can do some task. Any Broadcast for touch or any thing I can use. So that I can get in my application. Guide me.
I Do not want this thing
https://developer.android.com/reference/android/view/View.OnTouchListener.html
Thanks .
try this in your activity class,
public class TouchActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.d("Touch", "Touch");
sendBroadcast(new Intent("touch_event_has_occured"));
}
return super.onTouchEvent(event);
}
}
now in your global broadcast receiver you will get the brodcast
public class TouchReveiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("touch_event_has_occured"))
{
Log.d("Touch", "Touch");
}
}
}
don't forget to register the receiver with the action
Related
I create an Android app for a device without screen (not a phone). At startup my apps is launched and works correctly.
I try to catch the physical buttons event (down/up). It works but only one time when my apps starts. Then the events are not fire.
I have an idea : the apps is in the background and doesn't receive the events.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Context context = getApplicationContext();
//Start a service
Intent intent = new Intent(context,MainService.class);
context.startService(intent);
//finish();
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
//Send event to server
return true;
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
//Send event to server
return true;
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
//Send event to server
return true;
}
}
Do you have an idea ? I suppose I am not the first person with this problem ...
Regards,
Found the solution: a second app was catching and blocking? the events. I have removed the apps and it's ok now.
My activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
public void openService(View view) {
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, mWallpaperService.class));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
mWallpaperService
public class mWallpaperService extends WallpaperService {
#Override
public Engine onCreateEngine() {
return new mEngine();
}
private class mEngine extends Engine {
...
#Override
public void onTouchEvent(MotionEvent event) {
...
}
}
I get events in onTouchEvent here is it's description
/**
* Called as the user performs touch-screen interaction with the
* window that is currently showing this wallpaper. Note that the
* events you receive here are driven by the actual application the
* user is interacting with, so if it is slow you will get fewer
* move events.
*/
public void onTouchEvent(MotionEvent event) {
}
I think I'm loosing some touch events, my question is how do I add raw OnTouchListener?
I've tried to add implements View.OnTouchListener to all classes, but no luck, I don't have a view to register a new listener.
I want to use an extra physical button in my app. It is connected to the ear phones plug. I searched the web and didn't find specific answers. Maybe i didnt use the correct search terms?
Are there any tutorials available?
What events are triggered when such a button is pressed?
Is there any difference (in coding) between the original pressy and the cheap china things?
Is it possible to emulate this thing in the adk emulator?
I really want to use this thing in my app, but i dont have a clue how.
I need some help to get started
Any ideas?
Youre answer's helped me to get startet.
Now im am facing new problems, as the toasts i'll try to make to check the responses dont appear. Button press will just play and pause music with the samsung music player on the phone.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Toast.makeText(context, "onRecieve triggered", Toast.LENGTH_SHORT).show();
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();
Toast.makeText(context, "Action lauchched", Toast.LENGTH_SHORT).show();
if (action == KeyEvent.ACTION_DOWN) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
if (action == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
// do something
Toast.makeText(context, "Play Pause pressed!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
so it seems that the reciever code isnt reached.
in my activity there are following lines:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ZaehlenActivity.this);
setContentView(R.layout.zaehlen);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
registerReceiver(r, filter);
am i forgettig something? do i have to disable the standard earphone recognition somehow?
I think that earphone buttons are considered as MEDIA_BUTTONS so , you have to create a BroadcastReceiver which listen to android.intent.action.MEDIA_BUTTON and make a test statment to test if the action is an ACTION_DOWN , here's a suite of code it may helps :
This is the BroadcastReceiverwhich you have to declare it in AndroidManifest.xml , and make as Intent-filter : android.intent.action.MEDIA_BUTTON
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
#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) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
And this is an Activity which listen to the BroadcastReceiverand then make a Toastif one of the buttons was clicked :
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
registerReceiver(r, filter);
}
}
Hope it helps !
Seeing as Pressy is an external controller (so to speak), you'll need to use a BroadcastReceiver with an intent-filter for: android.intent.action.MEDIA_BUTTON So, for example:
Inside your AndroidManifest:
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
And your new receiver class:
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
// handle media button here
KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
// handle keydown etc
}
}
}
If you want to check single buttons (maybe on a cabled device, such as headphones, or wireless like Bluetooth), you could also use the onKeyDown and check for KeyEvent.KEYCODE_MEDIA_SOMEKEYIDENTIFIER Like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
return true;
case KeyEvent.KEYCODE_MEDIA_NEXT:
return true;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
return true;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
return true;
case KeyEvent.KEYCODE_MEDIA_REWIND:
return true;
case KeyEvent.KEYCODE_MEDIA_STOP:
return true;
// etc etc...
}
return false;
}
Hope this at least gets you started. Cheers.
I have a BroadCastReceiver that is listen for incoming short messages. In SmsReceiver I want to start an Activity to process the sms. In many situation the Activity is running while getting message and I don't want to start it again.
In fact I want to see that if that Activity is already running (visible or not killed yet) just take it to front and otherwise start it with new task.
Any idea?
I mixed some ideas from here and other places and finally solved the problem. I write the solution here for other people that may have similar situations:
In amy Activity:
static boolean isRunning = false;
#Override
public void onStart() {
super.onStart();
isRunning = true;
}
public void onStop() {
isRunning = false;
super.onStop();
}
public static boolean isRuuning() {
return isRunning;
}
#Override
public void onCreate(Bundle savedInstanceState) {
smsReceiver = new SmsReceiver();
smsReceiver.setActivity(this);
// ...
}
Inside my SmsReceiver:
static MyActivity myActivity;
public void setActivity(MyActivity MyActivity) {
SmsReceiver.myActivity = myActivity;
}
if( MyActivity.isRuuning() ) {
SmsReceiver.myActivity.receiveNewMessage(smsBody);
} else {
// Create an intent and start it
}
This works fine for me.
I am working on the application,I am having the requirement:if user long press the power button at any time and press again to start device,the activity should start from where the device was shut down.I dont know either this is a valid question or not.
I tried working with:
public class PowerMangerTestActivity extends Activity {
private static PowerManager objpowermanager;
private static PowerManager.WakeLock wl;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callTOWakeLock();
setContentView(R.layout.main);
}//end of onCreate
public void callTOWakeLock() {
// TODO Auto-generated method stub
objpowermanager=(PowerManager)getSystemService(Context.POWER_SERVICE);
wl=objpowermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "******MyTag****");
//You should acquire it when your app starts,
if(wl.isHeld())
{
wl.release();
}
wl.acquire();
}
#Override
public void onDestroy() {
wl.release();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
callTOWakeLock();
}
/*#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, PowerMangerTestActivity.class);
startActivity(i);
return true;
TextView tv=(TextView) findViewById(R.id.textview);
tv.setText("You press power button");
}
return super.dispatchKeyEvent(event);
}*/
}
but not getting expected behaviour.
I have tried to catch KeyEvent.KEYCODE_POWER,but not getting how to use for this scenario.
any suggestions?
thanks
This may be unacceptable to you, but you should NOT DO THIS.
It is going against a usability idea called "expected behavior".
You are doing something that a user is not expecting, and there is a good chance they are going to be pissed if you do this.
Just my two cents!
I would try and find another solution to this problem, such as caching useful data in phone storage, and recalling it when the application is resumed/started.
onPause still should be called when the power button is long pressed. The only case (as far as I know) where it won't be called is on a battery pull
It is reasonable for an emergency manager app to catch the power button event, so here's one solution, taken from Samsungs support database:
package com.samsung.lockscreenreceiver;
public class LockScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(Intent.ACTION_USER_PRESENT.equals(action)) {
// bring app to foreground
} else if(Intent.ACTION_SCREEN_OFF.equals(action) ) {
Toast.makeText(context, "screen on",Toast.LENGTH_LONG).show();
} else if (Intent.ACTION_SCREEN_ON.equals(action)) {
Toast.makeText(context, "screen off",Toast.LENGTH_LONG).show();
}
}
}
And the activity:
public class MyActivity extends Activity {
private BroadcastReceiver mReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReceiver = new LockScreenReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
//intentFilter.addAction(Intent.ACTION_SHUTDOWN); // won't work unless you're the device vendor
registerReceiver(mReceiver, intentFilter);
}
#Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
You catch power on/off and bring the activity to foreground. The activity then denies all focus changes using:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
//super.onWindowFocusChanged(hasFocus);
if(!hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
And you'll have to switch off screen lock, for example using:
https://play.google.com/store/apps/details?id=org.jraf.android.nolock&hl=en
The above app is not protected, so you can decompile it as usual. :)