您好,登錄后才能下訂單哦!
一:先來(lái)效果圖
二:實(shí)現(xiàn)步驟
1.xml布局實(shí)現(xiàn)
<?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:orientation="vertical" 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="edu.feicui.notification.MainActivity"> <Button android:id="@+id/btn_create" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送通知" android:textSize="25sp" /> </LinearLayout>
2.activity的實(shí)現(xiàn)
package edu.feicui.notification; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RemoteViews; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { /** * 通知欄Notification */ private NotificationManager mManager; private Notification mNotification; private PendingIntent mIntent; private String cll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cll = "今年27號(hào)過(guò)年喲!"; ButterKnife.bind(this); } @Override public void onContentChanged() { super.onContentChanged(); init(); } private void init() { //初始化通知欄管理者 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //意圖數(shù)組 Intent[] intents = {new Intent(this, NotificationAcitivity.class)}; //待處理意圖對(duì)象 mIntent = PendingIntent.getActivities(this, 0, intents, 0); //消息欄通知對(duì)象 mNotification = new Notification(); } @OnClick(R.id.btn_create) public void create() { //設(shè)置在通知欄的消息圖標(biāo) mNotification.icon = R.mipmap.logo_new; //設(shè)置在通知欄的信息內(nèi)容 mNotification.tickerText = "重大消息"; //設(shè)置默認(rèn)的聲音,此外還可以設(shè)置震動(dòng)(需加入權(quán)限) mNotification.defaults = Notification.DEFAULT_SOUND; //添加燈光 // mNotification.defaults=Notification.DEFAULT_LIGHTS; //不能刪除 mNotification.flags = Notification.FLAG_NO_CLEAR; //設(shè)置下拉時(shí)的顯示布局 RemoteViews convertView = new RemoteViews(getPackageName(), R.layout.layout_content); convertView.setImageViewResource(R.id.img, R.mipmap.logo_new); convertView.setTextViewText(R.id.txt, cll); mNotification.contentView = convertView; mNotification.contentIntent = mIntent; //發(fā)送通知 // 第一個(gè)參數(shù)唯一的標(biāo)識(shí)該Notification,第二個(gè)參數(shù)就是Notification對(duì)象 mManager.notify(1, mNotification); } }
3.AndroidManifest添加權(quán)限
<uses-permission android:name="android.permission.VIBRATE"/>
4.跳轉(zhuǎn)界面的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="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="20dp" android:text="今年27號(hào)過(guò)年喲!" /> </LinearLayout>
5.跳轉(zhuǎn)activity的實(shí)現(xiàn)
package edu.feicui.notification; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; import android.widget.TextView; /** * Created by Administrator on 2017-1-20. */ public class NotificationAcitivity extends Activity { private NotificationManager mManager; private int index = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); //初始化通知欄管理者 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); index = 2; mManager.cancelAll(); } }
簡(jiǎn)單粗暴實(shí)用,你值得擁有
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。