我正在玩穿戴SDK而且我注意到一种奇怪的行为:如果你试图通过手机中的BroadcastReceiver来佩戴语音输入,它就不起作用;似乎穿需要一个Activity来将数据返回到手机。
你之前有过这样的经历吗?
这是我用于我的活动的代码:
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();
}
}
}
答案 0 :(得分: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
。