android -- 通知栏的简单应用

来源:互联网 发布:知乎 教主 编辑:程序博客网 时间:2024/04/30 19:30

主要学习《第一行代码》,但是由于时间问题,会出现代码过时

在activity_main.xnm中定义一个按钮

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.adolph.notificationtest.MainActivity">        <Button        android:id="@+id/send_notice"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Send notice"/></RelativeLayout>


在MainActivity.java中进行逻辑部分编写

package com.adolph.notificationtest;public class MainActivity extends Activity implements View.OnClickListener{    private Button sendNotice;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }    @Override    public void onClick(View v){        switch (v.getId()) {            case R.id.send_notice:                NotificationManager manager = (NotificationManager)                        getSystemService(NOTIFICATION_SERVICE);                NotificationCompat.Builder mBuilder =                        new NotificationCompat.Builder(this)                                .setSmallIcon(R.drawable.qw)                                .setContentTitle("My notification")                                .setContentText("Hello World!");                manager.notify(1, mBuilder.build());                break;            default:                break;        }    }}


进一步完善,实现点击通知栏启动一个Activity

1、新建一个Test.java

2、给新建的Activity写一个布局文件,并且对Activity进行注册

3、修改MainActivity.java中代码如下

package com.adolph.notificationtest;public class MainActivity extends Activity implements View.OnClickListener{    private Button sendNotice;    /**     * ATTENTION: This was auto-generated to implement the App Indexing API.     * See https://g.co/AppIndexing/AndroidStudio for more information.     */    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }    @Override    public void onClick(View v){        switch (v.getId()) {            case R.id.send_notice:                NotificationManager manager = (NotificationManager)                        getSystemService(NOTIFICATION_SERVICE);                NotificationCompat.Builder mBuilder =                        new NotificationCompat.Builder(this)                                .setSmallIcon(R.drawable.qw)                                .setContentTitle("My notification")                                .setContentText("Hello World!");                Intent result = new Intent(this,Test.class);                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);                stackBuilder.addParentStack(Test.class);                stackBuilder.addNextIntent(result);                PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_CANCEL_CURRENT);                mBuilder.setContentIntent(pendingIntent);                manager.notify(1, mBuilder.build());                break;            default:                break;        }    }}












0 0