2013-04-09 106 views
0

好吧,继承人的问题。我已经制作了一个小部件,现在需要检测轻拍并双击它,当触发时应显示不同的视图。出于测试目的,我使用了日志记录来查看它是否正在工作。下面是我的代码: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; 
    } 

} 

控件提供者类的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中打印任何内容。这个问题在过去几天令我很烦恼。希望别人会知道我做错了什么。

编辑:忘了最重要的事情。使用在三星Galaxy王牌Android 2.2的API级别8(升级Froyo)和测试IM VE采用Android 2.3.6

+0

你不是在触摸你的'GestureDetector'的onTouch()事件? – Graeme 2013-04-09 16:11:53

+0

你能告诉我怎么样? – n00b 2013-04-09 16:14:52

回答

1

我做了一个widget

鉴于你的代码,我会假设你意味着其他Android开发人员称之为“应用小部件”。

,现在需要检测它

双水龙头水龙头,双击将是难以察觉。

下面是我的代码

你的代码的第一个块是一个活动,这是不是一个应用程序部件。

当用户点击应用程序窗口小部件时,您的第二个代码块会调用一个活动。这很好,但这不会帮助您检测到双击。事实上,它通常会阻止用户首先进行双击,因为活动将在第一次点击后接管屏幕。

我会劝你避免尝试使用应用小部件的双击。在应用程序小部件中有两个不同的项目来区分不同的事件(无论您想要单击一次,还是想要双击都可以)。