Reputation: 96
I am Developing an project in which i want diffrent task for diffrent time like if user button up after 5 milisecond then a code execute and if pressing time is more then that then a diffrent
actually em creating a chathead like in facebook chat
problem is this on ActionDown Event i want to open or launch my app
but problem is when user try to reallocate the chathead from one location to other the event fire like
This is my code
here in ACTION_DOWN event i want to intent to the app
public class ChadHead extends Service {
private final Handler mHandler = new Handler();
private Runnable mTimer1;
private WindowManager windowManager;
private ImageView chatHead;
Display display;
int width;
int count=0;
@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override
public void onCreate() {
super.onCreate();
mTimer1 = new Runnable() {
@Override
public void run() {
count++;
// TODO Auto-generated method stub
mHandler.postDelayed(this, 5000);
if (count == 1) {
mHandler.removeCallbacks(mTimer1);
// put your code here:
}
}
};
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@SuppressWarnings("deprecation")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
mHandler.postDelayed(mTimer1, 0);
/*Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}*/
Toast.makeText(getApplicationContext(), "TechnoRapper", 50)
.show();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
display = windowManager.getDefaultDisplay();
width = display.getWidth();
return true;
case MotionEvent.ACTION_UP:
count = 0;
if (initialTouchX < width / 2) {
initialTouchX = 0;
} else if (initialTouchX >= width / 2) {
initialTouchX = width;
}
return true;
}
return false;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null)
windowManager.removeView(chatHead);
}
}
Upvotes: 0
Views: 2926
Reputation: 1459
I faced it since 3 month ago, here is my solution:
private int count = 0;
Button btn;
private final Handler mHandler = new Handler();
private Runnable mTimer1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
mTimer1 = new Runnable() {
@Override
public void run() {
count++;
// TODO Auto-generated method stub
mHandler.postDelayed(this, 500);
if (count == 1) {
mHandler.removeCallbacks(mTimer1);
// put your code here:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.technorapper.technorappermapp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
}
}
};
btn.setOnTouchListener(new OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = (int) btn.getX();
initialY = (int) btn.getY();
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
mHandler.postDelayed(mTimer1, 0);
return true;
case MotionEvent.ACTION_UP:
count = 0;
return true;
case MotionEvent.ACTION_MOVE:
runOnUiThread(new Runnable() {
public void run() {
btn.setX(initialX
+ (int) (event.getRawX() - initialTouchX));
btn.setY(initialY
+ (int) (event.getRawY() - initialTouchY));
}
});
return true;
}
return false;
}
});
with this code you can move your chathead and the event start after 500 milisecond. No need to use OnClickListener, check if position not change so detect it as OnClickListener.
And if you want to stop OnTouch event when catch some action, just put 'boolean variable' in ACTION_MOVE
Upvotes: 2
Reputation: 771
You can use View.OnTouchListener and two timestamp to store the button click start (ACTION_DOWN) and end time (ACTION_UP). Then, difference between this 2 variables (end-start) will tell you the duration of button click time and you can execute code according to your preference.
test.setOnTouchListener(new OnTouchListener() {
private long start = 0;
private long end = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
this.start = System.currentTimeMillis();
} else if (event.getaction()==MotionEvent.ACTION_UP) {
this.end = System.currentTimeMillis();
}
}
});
Upvotes: 2
Reputation: 4117
i can't tell you the full exact code but i can explain the flow here, what i get from your answer is that you need to do different actions on the basis of last time the button is pressed, this is how you can do. You need two Date variables i.e. current and lastTime
DateTimeUtils.getCurrentUTCFormattedDateTime()
store the lastTime in preferences on action down fetch the last time from preferences and get the current time and subtract it, to get the difference.
long difference = (current.getTime() - lastSaved.getTime()) / 1000;
give you time difference in seconds, then you decide what to do. Hope it help :)
Upvotes: 0
Reputation: 2124
If you want do have custom durations you have to program it by yourself.
However it is recommended to use the android build in functions. There are a View.OnLongClickListener and a View.OnClickListener to distinguish from two different durations for clicking on views.
Upvotes: 1