package com.example.admin.application;import android.app.Activity;import android.app.Notification;import android.app.PendingIntent;import android.content.Intent;import android.graphics.BitmapFactory;import android.graphics.Color;import android.net.Uri;import android.os.Bundle;import android.support.v4.app.RemoteInput;import android.support.wearable.view.WatchViewStub;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationManagerCompat;import android.support.v4.app.NotificationCompat.WearableExtender;import android.widget.Toast;public class MainActivity extends Activity {    // Key for the string that's delivered in the action's intent    private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";    private TextView mTextView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {            @Override            public void onLayoutInflated(WatchViewStub stub) {                mTextView = (TextView) stub.findViewById(R.id.text);                Button button = (Button) stub.findViewById(R.id.button);                button.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                //        createNotification("测试1","Fiberhome");                //        createSpecialNotification("测试2","Fiberhome");                //        createNotificationWithVoiceInput("语音输入","Fiberhome");                //        createWearNotification("测试4","Fiberhome");                //        createNotificationWithAction(""测试5","Fiberhome");                        createPagesNotification("测试5","Fiberhome");                    }                });                Button button2 = (Button) stub.findViewById(R.id.button2);                button2.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                    }                });            }        });    }    /**     * 为 Notification 添加页面     * @param title     * @param content     */    private void createPagesNotification(String title, String content) {    }    /**     * 在 Notifcation 中接收语音输入     * @param title     * @param content     */    private void createNotificationWithVoiceInput(String title, String content) {        String replyLable=getResources().getString(R.string.reply_label);        String[] choices=getResources().getStringArray(R.array.reply_choices);        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)                .setLabel(replyLable)                .setChoices(choices)                .build();        // Create an intent for the reply action        Intent replyIntent = new Intent(this, ViewEventActivity.class);        PendingIntent replyPendingIntent =                PendingIntent.getActivity(this, 0, replyIntent,                        PendingIntent.FLAG_UPDATE_CURRENT);        // Create the reply action and add the remote input        NotificationCompat.Action action =                new NotificationCompat.Action.Builder(R.drawable.ic_fiber_new_blue_300_18dp,                        getString(R.string.label), replyPendingIntent)                        .addRemoteInput(remoteInput)                        .build();        // Build the notification and add the action via WearableExtender        Notification notification =                new NotificationCompat.Builder(this)                        .setSmallIcon(R.drawable.ic_event_red_200_18dp)                        .setContentTitle(title)                        .setContentText(content)                        .setDefaults(Notification.DEFAULT_ALL)                        .extend(new WearableExtender().addAction(action))                        .build();        // Issue the notification        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        int notificationId=004;        notificationManager.notify(notificationId, notification);        Toast.makeText(this,getMessageText(replyIntent),Toast.LENGTH_SHORT).show();    }    /**     * @param title     * @param content     */    private void createWearNotification(String title, String content) {        // Create a WearableExtender to add functionality for wearables        NotificationCompat.WearableExtender wearableExtender =                new NotificationCompat.WearableExtender()                        .setHintHideIcon(true)                        .setBackground(BitmapFactory.decodeResource(getResources(),R.mipmap.notification_icon1));        // Create a NotificationCompat.Builder to build a standard notification        // then extend it with the WearableExtender        Notification notif = new NotificationCompat.Builder(this)                .setContentTitle(title)                .setContentText(content)                .setDefaults(Notification.DEFAULT_ALL)                .setSmallIcon(R.drawable.ic_fiber_new_blue_300_18dp)                .extend(wearableExtender)                .build();        // Get an instance of the NotificationManager service        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        // Build the notification and issues it with notification manager.        int notificationId=003;        notificationManager.notify(notificationId, notif);    }    /**     * 添加一个Big View     * @param title     * @param content     */    private void createNotificationWithBigView(String title, String content) {        // Specify the 'big view' content to display the long        // event description that may not fit the normal content text.        Intent actionIntent = new Intent(this, ViewEventActivity.class);        PendingIntent actionPendingIntent =                PendingIntent.getActivity(this, 0, actionIntent,                        PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.BigTextStyle bigTextStyle=new NotificationCompat.BigTextStyle();        bigTextStyle.bigText(content);        NotificationCompat.Builder notificationBuilder =                new NotificationCompat.Builder(this)                        .setSmallIcon(R.drawable.ic_event_red_200_18dp)                        .setLargeIcon(BitmapFactory.decodeResource(                                getResources(), R.mipmap.notification_icon1))                        .setContentTitle(title)                        .setContentText(content)                        .setContentIntent(actionPendingIntent)                        .setDefaults(Notification.DEFAULT_ALL)                        .addAction(R.drawable.ic_add_location_black_18dp,                                getString(R.string.map), actionPendingIntent)                        .setStyle(bigTextStyle);        // Get an instance of the NotificationManager service        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        // Build the notification and issues it with notification manager.        int notificationId=003;        notificationManager.notify(notificationId, notificationBuilder.build());    }    /**     * Obtain the intent that started this activity by calling     * Activity.getIntent() and pass it into this method to     * get the associated voice input string.     */    private CharSequence getMessageText(Intent intent) {        Bundle bundle=RemoteInput.getResultsFromIntent(intent);        if (bundle != null) {            return bundle.getCharSequence(EXTRA_VOICE_REPLY);        }        return null;    }    /**     * 可穿戴式独有的 Actions     * @param title     * @param content     */    private void createSpecialNotification(String title, String content) {        // Create an intent for the reply action        Intent actionIntent = new Intent(this, ViewEventActivity.class);        PendingIntent actionPendingIntent =                PendingIntent.getActivity(this, 0, actionIntent,                        PendingIntent.FLAG_UPDATE_CURRENT);        // Create the action        NotificationCompat.Action action =                new NotificationCompat.Action.Builder(R.drawable.ic_android_black_18dp,                        getString(R.string.label), actionPendingIntent)                        .build();        // Build the notification and add the action via WearableExtender        Notification notification =                new NotificationCompat.Builder(this)                        .setSmallIcon(R.drawable.ic_message_light_blue_300_18dp)                        .setContentTitle(title)                        .setContentText(content)                        .setDefaults(Notification.DEFAULT_ALL)                        .extend(new WearableExtender().addAction(action))                        .build();        // Get an instance of the NotificationManager service        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        // Build the notification and issues it with notification manager.        int notificationId=003;        notificationManager.notify(notificationId, notification);    }    /**     * 创建一个Notification,添加Action按钮     * @param location     * @param content     */    private void createNotificationWithAction(String location, String content) {        int notificationId=002;        Intent mapIntent=new Intent(Intent.ACTION_VIEW);        Uri geoUri=Uri.parse("geo:0,0?q="+Uri.encode(location));        mapIntent.setData(geoUri);        PendingIntent mapPendingIntent =                PendingIntent.getActivity(this, 0, mapIntent, 0);        NotificationCompat.Builder notificationBuilder =                new NotificationCompat.Builder(this)                        .setSmallIcon(R.drawable.ic_add_location_black_18dp)                        .setContentTitle(location)                        .setContentText(content)                        .setDefaults(Notification.DEFAULT_ALL)                        .addAction(R.mipmap.ic_launcher,                                getString(R.string.map), mapPendingIntent);        // Get an instance of the NotificationManager service        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        // Build the notification and issues it with notification manager.        notificationManager.notify(notificationId, notificationBuilder.build());    }    /**     * 创建一个Notification     * @param eventTitle     * @param eventLocation     */    private void createNotification(String eventTitle, String eventLocation) {        int notificationId = 001;        // Build intent for notification content        Intent viewIntent = new Intent(this, ViewEventActivity.class);        PendingIntent viewPendingIntent =                PendingIntent.getActivity(this, 0, viewIntent, 0);        NotificationCompat.Builder notificationBuilder =                new NotificationCompat.Builder(this)                        .setSmallIcon(R.mipmap.notification_icon1)                        .setContentTitle(eventTitle)                        .setContentText(eventLocation)                        .setDefaults(Notification.DEFAULT_ALL)                        .setContentIntent(viewPendingIntent);        // Get an instance of the NotificationManager service        NotificationManagerCompat notificationManager =                NotificationManagerCompat.from(this);        // Build the notification and issues it with notification manager.        notificationManager.notify(notificationId, notificationBuilder.build());    }}

更多相关文章

  1. Android语音识别功能使用
  2. android 输入对话框
  3. Android菜单实例
  4. Android语音识别
  5. Android(安卓)中的 requestWindowFeature
  6. 在/external/tinyalsa/pcm.c 添加android log
  7. Android语音识别
  8. Android语音识别
  9. android 按钮按下时改变字体颜色

随机推荐

  1. FFmpeg 调用 Android(安卓)MediaCodec 进
  2. Android(安卓)程序员的进阶之路(转)
  3. 开源分享!2021年最新 Android(安卓)资料汇
  4. Android(安卓)消息机制 你了解多少
  5. 一步教你超简单设置Android(安卓)Studio
  6. Android(安卓)NDK 实现视音频播放器源码
  7. Android高级进阶之路【六】Android(安卓)
  8. android下载封装类
  9. android 开发 制作弹出等待进度条
  10. Android(安卓)重力感应选号码