我可以在不使用notificationcompat.builder的情况下使用android佩戴语音回复吗?

时间:2015-06-12 07:26:00

标签: android android-layout android-intent android-activity wear-os

我想使用android中的语音回复按钮点击我的一个页面在android穿戴应用程序。我已经看到网上的所有帮助,但它说我只有在必须使用语音回复时才需要创建通知。 我提到的代码之一如下:

import android.app.Activity;
import android.app.Notification;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.RemoteInput;
import android.widget.Toast;

public class MainActivity extends Activity {
public static final int NOTIFICATION_ID = 1;
public static final String ACTION_REPLY_TAPPED =
        "com.learnandroidwear.ACTION_REPLY_TAPPED";
public static final String EXTRA_REPLY = "com.learnandroidwear.EXTRA_REPLY";
private IntentFilter intentFilter;
// Create a BroadcastReceiver when a voice input or pre-defined response is delivered
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_REPLY_TAPPED)) {
            Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
            CharSequence reply = remoteInput.getCharSequence(EXTRA_REPLY);
            Toast.makeText(getBaseContext(), "Response: "
                    + reply, Toast.LENGTH_LONG).show();
        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
// Cancel all previous notifications and generate unique ids
    NotificationManagerCompat.from(this).cancelAll();
// Create an Intent for the reply action
    Intent replyIntent = new Intent(ACTION_REPLY_TAPPED);
// Create a pending intent that will be used to send a broadcast when a reply is sent
    PendingIntent replyPendingIntent =
            PendingIntent.getBroadcast(this, 0, replyIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
// Create RemoteInput object for a voice and pre-defined replies
    String[] replyChoices = getResources().getStringArray(
            R.array.reply_choices);
    int i;
// Convert ASCII Emoji UNICODE.
// For more Emojis, see http://apps.timwhitlock.info/emoji/tables/unicode.
    for (i=0; i<replyChoices.length; i++)
    {
        if (replyChoices[i].contains(":)")) replyChoices[i] =
                replyChoices[i].replace(":)", new String(
                        Character.toChars(0x1F60A)));
        if (replyChoices[i].contains(":(")) replyChoices[i] =
                replyChoices[i].replace(":(", new String(
                        Character.toChars(0x1F61E)));
        if (replyChoices[i].contains(":P")) replyChoices[i] =
                replyChoices[i].replace(":P", new String(
                        Character.toChars(0x1F61C)));
    }
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_REPLY)
            .setLabel("Choice")
            .setChoices(replyChoices)
            .build();
// Create a wearable only "Reply" action
    NotificationCompat.Action replyAction =
            new                 NotificationCompat.Action.Builder(R.drawable.ic_action_reply,
                    "Reply", replyPendingIntent)
                    .addRemoteInput(remoteInput)
                    .build();
// Create a wearable extender for a notification and add the "replyAction"
    NotificationCompat.WearableExtender wearableExtender =
            new NotificationCompat.WearableExtender()
                    .addAction(replyAction);
// Create a notification and extend it for the wearable
    Notification notification =
            new NotificationCompat.Builder(this)
                    .setContentTitle("From Brandon")
                    .setContentText("Are you ready to go?")
                    .setSmallIcon(R.drawable.ic_action_search)
                    .setContentIntent(PendingIntent.getActivity(this, 0,
                            new Intent(this,
                                    MainActivity.class), 0))
                    .extend(wearableExtender)
                    .build();
// Get an instance of the NotificationManagerCompat service
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
// Dispatch the notification
    notificationManager.notify(NOTIFICATION_ID, notification);
// Intent to filter for receiving voice input or pre-defined response
// from notification
    intentFilter = new IntentFilter();
    intentFilter.addAction(ACTION_REPLY_TAPPED);
}
@Override
protected void onResume() {
    super.onResume();
// Register the receiver while the activity is visible
    registerReceiver(intentReceiver, intentFilter);
}
@Override
protected void onPause() {
    super.onPause();
// Unregister the receiver if the activity is not in the foreground
    unregisterReceiver(intentReceiver);
    }
}

我希望你得到我想说的话。 不确定是否可能,是的,我是Android开发的新手。 在此先感谢:)

0 个答案:

没有答案