Day03

作者: 鴻9527 | 来源:发表于2019-07-08 21:31 被阅读0次

    Day03

    提示信息代码

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    }
    
    
    
    
    public void click(View view) {
    
    
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            Notification.Builder builder = new Notification.Builder(this);
    
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setTicker("提示收到了一天消息");
            builder.setContentTitle("天气预报");
            builder.setContentText("正文:今天天气晴,东南方转西北风5~6级");
            builder.setContentInfo("附加消息");
            builder.setDefaults(Notification.DEFAULT_ALL);
            builder.setAutoCancel(true);
    
            Notification build = builder.build();
            manager.notify(200,build);
    
        }
    

    }

    效果图

    aa.gif

    大图提示信息代码

    public void big(View view) {

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
        Notification.Builder builder = new Notification.Builder(this);
    
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("大图显示");
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
    
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setStyle(bigPictureStyle);
        manager.notify(4,builder.build());
    
    }
    

    效果图

    bb.gif

    提示信息跳转

    public void click(View view) {

            //通知管理者,发送通知。取消通知
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            //创建构造者
            Notification.Builder builder = new Notification.Builder(this);
            //设置属性  setSamllIcon该属性必须设置
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setTicker("提示收到了一天消息");
            builder.setContentTitle("天气预报");
            builder.setContentText("正文:今天天气晴,东南方转西北风5~6级");
            builder.setContentInfo("附加消息");
            builder.setDefaults(Notification.DEFAULT_ALL);
            builder.setAutoCancel(true);
    
            //跳转到其他页面
            //创建意图对象
            Intent intent = new Intent(this, MainActivity.class);
            //intent对象转成pendingintent
            PendingIntent pendingIntent=PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_ONE_SHOT);
            //设置跳转
            builder.setContentIntent(pendingIntent);
    
            //发送通知
          Notification build = builder.build();
            manager.notify(200,build);
    
        }
    

    效果图

    cc.gif

    列表消息

    public void list(View view) {

        //获得通知的管理对象
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //通知的穿件对象
        Notification.Builder builder = new Notification.Builder(this);
        //设置小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置标题
        builder.setContentTitle("标题");
        //设置内容
        builder.setContentText("内容");
    
        builder.setAutoCancel(true);
    
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("1");
        inboxStyle.addLine("2");
        inboxStyle.addLine("3");
        inboxStyle.addLine("4");
        inboxStyle.addLine("5");
        inboxStyle.addLine("6");
        inboxStyle.addLine("7");
        inboxStyle.addLine("8");
        inboxStyle.addLine("9");
        builder.setStyle(inboxStyle);
        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setFullScreenIntent(pendingIntent,true);
        builder.setContentIntent(pendingIntent);
        manager.notify(9,builder.build());
    
    }
    

    效果图

    dd.gif

    自定义消息

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是自定义"
            android:textSize="25dp"/>
    
        <ImageView
            android:id="@+id/image_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_toRightOf="@id/text_one"/>
    
    </RelativeLayout>
    
    public void zidingyi(View view) {
    
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            Notification.Builder builder = new Notification.Builder(this);
    
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("标题");
            builder.setContentText("自定义");
    
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_zidingyi);
            remoteViews.setTextViewText(R.id.text_one,"这是自定义");
            remoteViews.setImageViewResource(R.mipmap.ic_launcher,R.mipmap.ic_launcher);
            builder.setContent(remoteViews);
            manager.notify(4,builder.build());
    
        }
    

    效果图

    ee.gif

    相关文章

      网友评论

          本文标题:Day03

          本文链接:https://www.haomeiwen.com/subject/vptahctx.html