2014-09-21 2 views
0

Я играю с изнашиванием SDK, и я заметил странное поведение: если вы попытаетесь поймать голосовые сообщения от ношения BroadcastReceiver на своем телефоне, это не сработает; кажется, что износ требует Activity для возврата данных на телефон.Android Wear не возвращает речевые данные

Испытывали ли вы это раньше?

Вот код, я использую для моей деятельности:

package com.tizianobasile.weartest; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.NotificationManagerCompat; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.app.RemoteInput; 
import android.view.View; 
import android.widget.Button; 


public class MyActivity extends Activity { 
    private Button notificationBtn; 
    public static final String ACTION_DEMAND = "com.tizianobasile.weartest.MyActivity.ACTION_DEMAND"; 
    public static final String EXTRA_MESSAGE = "message"; 
    public static final String EXTRA_VOICE_REPLY = "reply"; 

    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender().setHintShowBackgroundOnly(true); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
     notificationBtn = (Button) findViewById(R.id.notificationBtn); 
     final Activity a = this; 
     notificationBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       createNotification(); 
      } 
     }); 
    } 

    private void createNotification(){ 
     Intent demandIntent = new Intent(this, DemandReceiver.class) 
       .putExtra(EXTRA_MESSAGE, "Reply Selected") 
       .setAction(ACTION_DEMAND); 
     PendingIntent demandPendingIntent = PendingIntent.getActivity(this, 0, demandIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     String replyLabel = getResources().getString(R.string.app_name); 
     String[] replyActions = {"Yes", "No"}; 
     RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) 
       .setLabel(replyLabel) 
       .setChoices(replyActions) 
       .build(); 

     NotificationCompat.Action replyAction = 
       new NotificationCompat.Action.Builder(R.drawable.ic_reply, "Reply", demandPendingIntent) 
         .addRemoteInput(remoteInput) 
         .build(); 

     NotificationCompat.WearableExtender wearableExtender = 
       new NotificationCompat.WearableExtender() 
         .addAction(replyAction); 

     long[] vibration = {0, 1000}; 

     Notification notification = 
       new NotificationCompat.Builder(this) 
         .setContentTitle("Hello Wear") 
         .setContentText("You received a notification") 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setVibrate(vibration) 
         .extend(wearableExtender) 
         .build(); 

     NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); 

     notificationManager.notify(1, notification); 
    } 
} 

И есть BroadcastReceiver:

package com.tizianobasile.weartest; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.RemoteInput; 
import android.util.Log; 
import android.widget.Toast; 

public class DemandReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("WEAR", "Context: " + context.toString()); 
     Log.d("WEAR", "Intent: " + intent.toString()); 
     Log.d("WEAR", "Action" + intent.getAction()); 
     if(intent.getAction().equals(MyActivity.ACTION_DEMAND)){ 
      String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); 
      Log.d("WEAR", "Message from intent: " + message); 
      Toast.makeText(context, "Message from intent: " + message, Toast.LENGTH_LONG).show(); 
      Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); 
      CharSequence reply = remoteInput.getCharSequence(MyActivity.EXTRA_VOICE_REPLY); 
      Log.d("WEAR", "User replies: " + reply); 
      Toast.makeText(context, "User replies: " + reply, Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

ответ

2

При создании вашего demandPendingIntent вы в основном говорят, что это будет Activity:

Intent demandIntent = new Intent(this, DemandReceiver.class) 
      .putExtra(EXTRA_MESSAGE, "Reply Selected") 
      .setAction(ACTION_DEMAND); 
    PendingIntent demandPendingIntent = PendingIntent.getActivity(this, 0, demandIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Вместо использования:
PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
вам нужно использовать:
PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

Это позволит системе знать, что вы хотите, чтобы справиться с этим PendingIntent по Broadcastreceiver.

+0

Большое вам спасибо, это работает! – basteez

Смежные вопросы