notification

来源:互联网 发布:tk域名注册不了 编辑:程序博客网 时间:2024/06/05 17:14

看了郭霖大神的《第一行代码》学习感觉挺好,不过有些东西确实老了,notification的简单用法在此记下,其实很简单,直接Mark一下代码:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.administrator.notification.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/sendInfo"        android:text="sendInfo"        /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="hello"/></LinearLayout>

MainActivity

package com.example.administrator.notification;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    private Button sendInfo;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendInfo= (Button) findViewById(R.id.sendInfo);        sendInfo.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);                Notification.Builder builder = new Notification.Builder(MainActivity.this);                builder.setContentInfo("补充内容");                builder.setContentText("主内容区");                builder.setContentTitle("通知标题");                builder.setSmallIcon(R.mipmap.ic_launcher);                builder.setTicker("新消息");                builder.setAutoCancel(true);                builder.setWhen(System.currentTimeMillis());                Intent intent = new Intent(MainActivity.this, NewActivity.class);                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);                builder.setContentIntent(pendingIntent);                Notification notification = builder.build();                manager.notify(1, notification);            }        });    }}

NewActivity

package com.example.administrator.notification;import android.app.Activity;import android.os.Bundle;/** * Created by Administrator on 2016/8/27. */public class NewActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.newd);    }}

显示效果:
这里写图片描述
此图片来自网络,我手机不能截通知栏的屏~~

0 0
原创粉丝点击