傳送支援手錶的notification,然後再notification 裡面語音回覆
阿新 • • 發佈:2018-12-04
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 可以接收通知就可以了