Хорошо, это проблема. Я сделал виджет, и теперь вам нужно обнаружить кран и дважды коснуться его, который при запуске должен показывать другое представление. В целях тестирования я использовал журнал, чтобы узнать, работает ли он. Ниже мой код:Android Widget обнаруживает кран и двойной кран
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;
/**
* @Author Jan Zivkovic
*
* NE DELA, OVERRIDA AMPAK NE DELA!
*/
public class UpdateWidget extends Activity implements OnGestureListener{
private GestureDetector gesture;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Log.w("TEST", "started");
gesture = new GestureDetector(this);
//set the on Double tap listener
gesture.setOnDoubleTapListener(new OnDoubleTapListener()
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
//set text color to green
//tvTap.setTextColor(0xff00ff00);
//print a confirmation message
//tvTap.setText("The screen has been double tapped.");
Log.w("TEST", "Double tap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
//if the second tap hadn't been released and it's being moved
if(e.getAction() == MotionEvent.ACTION_MOVE)
{
//set text to blue
//tvTapEvent.setTextColor(0xff0000ff);
//print a confirmation message and the position
//tvTapEvent.setText("Double tap with movement. Position:\n"
// + "X:" + Float.toString(e.getRawX()) +
// "\nY: " + Float.toString(e.getRawY()));
}
else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen
{
//setContentView();
}
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
//set text color to red
//tvTap.setTextColor(0xffff0000);
//print a confirmation message and the tap position
//tvTap.setText("Double tap failed. Please try again.");
Log.w("TEST", "Single tap");
return false;
}
});
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
}
Widget класс Provider, метод OnUpdate (срабатывают каждые 5 минут)
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
Intent updateIntent = new Intent(context, UpdateWidget.class);
// Identifies the particular widget...
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
updateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Make the pending intent unique...
updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, updateIntent, 0);
//context.startActivity(updateIntent);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);//R.layout.widget_layout
views.setTextViewText(R.id.subject1, "subject1 test");
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Log.w("Schedule", "Widget Updated = " + appWidgetId);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Но Херес, что он получил, чтобы быть до сих пор. Нормальный, когда вы нажимаете или дважды нажимаете на виджет, телефон вибрирует. Используя это, телефон больше не вибрирует, когда он постучал, и ничего не печатает в logcat. Эта проблема раздражает меня последние пару дней. Надеюсь, кто-то еще узнает, что я делаю неправильно.
EDIT: Забыл самое главное. Im использованием Android уровня 2,2 апи 8 (Froyo) и тестирование на Samsung Galaxy Ace VE с Android 2.3.6
Вы не подаете события onTouch() на свой 'GestureDetector'? – Graeme
Не могли бы вы показать мне, как? – n00b