Notification的功能和用法

来源:互联网 发布:linux boot 编辑:程序博客网 时间:2024/05/18 02:55

java代码

/*MainActivity.java*/package com.example.zhang.notificationtest;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends AppCompatActivity {    static final int NOTIFICATION_ID = 0x123;    NotificationManager notificationManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取系统的NotificationManager服务        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });    }    public void send (View source){        //创建一个起动其他Activity的Intent        Intent intent = new Intent(MainActivity.this, OtherActivity.class);        //PendingIntent即将发生的意图,PendingIntent这个类用于处理即将发生的事情。        // 比如在通知Notification中用于跳转页面,但不是马上跳转。        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);        Notification notify = new Notification.Builder(this)                //设置打开该通知            .setAutoCancel(true)                //设置显示在状态栏的通知提示信息            .setTicker("有新消息")                //设置通知图标            .setSmallIcon(R.drawable.notify)                //设置通知内容的标题            .setContentTitle("一条新通知")                //设置通知内容            .setContentText("恭喜你,您加薪了,工资增加20%")                //设置使用默认的声音,默认的闪光灯            .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)                //设置通知的自定义声音           // .setSound(Uri.parse("android.resource://com.example.zhang.notificationtest/ + R.raw.msg"))            .setWhen(System.currentTimeMillis())                //设置通知将要启动的程序的Intent            .setContentIntent(pi)            .build();           //发送通知            notificationManager.notify(NOTIFICATION_ID, notify);    }    public void del (View v){        //取消通知        notificationManager.cancel(NOTIFICATION_ID);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}/*OtherActivity*/package com.example.zhang.notificationtest;import android.app.Activity;import android.os.Bundle;/** * Created by zhang on 2015/12/18. */public class OtherActivity extends Activity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        //设置该Activity显示的页面        setContentView(R.layout.other);    }}

xml代码

<?xml version="1.0" encoding="utf-8"?><!--content_main.xml--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.zhang.notificationtest.MainActivity"    tools:showIn="@layout/activity_main">    <Button        android:onClick="send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="send" />    <Button        android:onClick="del"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="cancel"/></LinearLayout><!--other.xml--><?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="vertical">    <!-- 定义一个ImageView -->    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@drawable/swift"        android:layout_gravity="center_horizontal"        /></LinearLayout><!--string.xml--><resources>    <string name="app_name">NotificationTest</string>    <string name="action_settings">Settings</string>    <string name="other_activity">Notification启动的Activity</string></resources><!--AndroidMainifest.xml--><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.zhang.notificationtest">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!--这里是添加其他Activity的格式-->        <activity            android:name=".OtherActivity"            android:label="@string/other_activity">        </activity>        <!-- 添加操作闪光灯的权限 -->        <uses-permission android:name="android.permission.FLASHLIGHT"/>        <!-- 添加操作振动器的权限 -->        <uses-permission android:name="android.permission.VIBRATE"/>    </application></manifest>

图片
这里写图片描述

这里写图片描述

0 0