2014-12-19 86 views
0

我有隻有一個按鈕的控件。我讀聽者應該設置這樣的:設置onClickListener一個按鈕控件

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 

views.setOnClickPendingIntent(R.id.button1, pendingIntent); 

但我有這樣的代碼:

  try { 
      sr = SpeechRecognizer 
        .createSpeechRecognizer(context); 
      sr.setRecognitionListener(new MainActivity().new listener()); 

      Intent intent = new Intent(
        RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
      intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 
        "voice.recognition.test"); 
      intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
      sr.startListening(intent); 

     } catch (Exception e) { 
      Toast.makeText(context, "Exc: " + e, Toast.LENGTH_LONG).show(); 
     } 

這應該是onClick事件..我不能使這一等待意圖..

任何想法? :)

回答

0

您可以在您的窗口小部件提供程序中處理PendingIntent

以下是例子:

1)設置意圖按鈕點擊:

final Intent intentButton = new Intent(context, MyWidgetProvider.class); 

intentButton.setAction(ACTION_MY_BUTTON); 

final PendingIntent pendingIntentButton = 
     PendingIntent.getBroadcast(context, 0, intentButton, PendingIntent.FLAG_UPDATE_CURRENT); 

remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntentButton); 

2)處理意圖在MyWidgetProvider

@Override 
public void onReceive(final Context context, final Intent intent){ 
    super.onReceive(context, intent); 

    final String action = intent.getAction(); 

    if (ACTION_MY_BUTTON.equals(action)) { 
     // Do your stuff here! 
    } 
相關問題