发送支持手表的notification,然后再notification 里面语音回复

1: 发送的activity

package tech.androidstudio.notificationforresume;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnStart(View view) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Start")
                .setContentText("Resume")
                .setDefaults(NotificationCompat.DEFAULT_VIBRATE);//设置震动

//                    Intent voiceReply = new Intent();
//                    intent.setAction("android.intent.action.CALL");
//                    intent.setData(Uri.parse("tel://17761838076"));

        //单独的给可穿戴的启动activity ,然后再在DetailActivity里面接收数据
        Intent voiceReply =new Intent(this,DetailActivity.class);

        //语音回复的输入
        String replyLabel = "语音回复";
        RemoteInput remoteInput = new RemoteInput.Builder(MainActivity.EXTRA_VOICE_REPLY)
                .setLabel(replyLabel)
                .build();

        PendingIntent pendingIntent = PendingIntent.getActivity(this,1998,voiceReply,PendingIntent.FLAG_UPDATE_CURRENT) ;
        NotificationCompat.Action notificationAction =
                new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,"语音回复",pendingIntent)
                        .addRemoteInput(remoteInput)
                        .build();


//                    builder.addAction(R.mipmap.ic_launcher,"action",pendingIntent);
        builder.setContentIntent(pendingIntent);

        //设置上面的notificatoinAction 只在手表端显示
        builder.extend(new NotificationCompat.WearableExtender().addAction(notificationAction));

        Notification n = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(998,n);
    }
}


2: 接收的activity

package tech.androidstudio.notificationforresume;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v4.app.RemoteInput;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;


import java.util.List;


public class DetailActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);


        Intent reply = getIntent();
        CharSequence voiceReply = getMessageText(reply);
        String sender = reply.getStringExtra("SENDER");
        Log.d("kodulf", "收到="+voiceReply+" sender="+sender);


        //这里的 voiceReply 是我们需要的
        String startMyResume="打开我的简历";
        String startResume ="打开简历";
        String stringVoiceReply = String.valueOf(voiceReply);


        if(startMyResume.equals(stringVoiceReply)||startResume.equals(stringVoiceReply)){
            Log.d("kodulf", "收到="+voiceReply+" sender="+sender);


            openApp("tech.androidstudio.myresume");
        }


    }


    private CharSequence getMessageText(Intent intent) {
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(MainActivity.EXTRA_VOICE_REPLY);
        }
        return null;
    }




    private void openApp(String packageName) {
        Context context = this;
        PackageInfo pi = null;
        PackageManager pm = context.getPackageManager();
        try {
            pi = pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);


        List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);


        ResolveInfo ri = apps.iterator().next();
        if (ri != null ) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;


            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);


            ComponentName cn = new ComponentName(packageName, className);


            intent.setComponent(cn);
            startActivity(intent);
        }
    }
}


3: 如果出现了手机端有通知,但是手表端没有的情况,那么需要设置一下:

打开管理软件Android Wear,这个时候界面上面会有一个通知的设置的,点击进去以后,允许Android Wear 可以接收通知就可以了





猜你喜欢

转载自blog.csdn.net/Kodulf_007/article/details/72085604