国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解Android中Notification的使用方法

2020-04-11 11:08:04
字體:
來源:轉載
供稿:網友

      在消息通知的時候,我們經常用到兩個控件Notification和Toast。特別是重要的和需要長時間顯示的信息,用Notification最合適不過了。他可以在頂部顯示一個圖標以標示有了新的通知,當我們拉下通知欄的時候,可以看到詳細的通知內容。
      最典型的應用就是未看短信和未接來電的顯示,還有QQ微信,我們一看就知道有一個未接來電或者未看短信,收到QQ離線信息。同樣,我們也可以自定義一個Notification來定義我們自己的程序想要傳達的信息。

Notification我把他分為兩種,一種是默認的顯示方式,另一種是自定義的,今天為大家講述默認的顯示方式
1、程序框架結構圖如下


2、布局文件 main.xml 源碼如下

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  > <TextView   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:gravity="center"  android:textColor="#EEE"  android:textStyle="bold"  android:textSize="25sp"  android:text="NotificationDemo實例" /> <Button  android:id="@+id/btnSend"  android:text="send notification"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="center"/>  </LinearLayout> 

3、MainActivity.java源碼如下:

package com.andyidea.notification;  import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;  public class MainActivity extends Activity {  private Button btnSend;    //定義BroadcastReceiver的action  private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action";    /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);      btnSend = (Button)findViewById(R.id.btnSend);   btnSend.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {     Intent intent = new Intent();     intent.setAction(NotificationDemo_Action);     sendBroadcast(intent);    }   });  }   } 

4、布局文件 secondlayou.xml 源碼如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <TextView   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:gravity="center"  android:textColor="#EEE"  android:textStyle="bold"  android:textSize="25sp"  android:text="顯示通知界面" /> <Button  android:id="@+id/btnCancel"  android:text="cancel notification"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="center" />  </LinearLayout> 

5、SecondActivity.java源碼如下:

package com.andyidea.notification;  import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;  public class SecondActivity extends Activity {   private Button btnCancel;  //聲明Notification  private Notification notification;  //聲明NotificationManager  private NotificationManager mNotification;  //標識Notification的ID  private static final int ID = 1;    @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.secondlayout);      btnCancel = (Button)findViewById(R.id.btnCancel);   //怎樣獲得NotificationManager的實例?   String service = NOTIFICATION_SERVICE;   mNotification = (NotificationManager)getSystemService(service);      //獲得Notification的實例   notification = new Notification();      //設置該圖標 會在狀態欄顯示   int icon = notification.icon = android.R.drawable.stat_sys_phone_call;   //設置提示信息   String tickerText = "Test Notification";   //設置顯示時間   long when = System.currentTimeMillis();   notification.icon = icon;   notification.tickerText = tickerText;   notification.when = when;      Intent intent = new Intent(this, MainActivity.class);   PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);   notification.setLatestEventInfo(this, "消息", "SMS Android", pi);   mNotification.notify(ID, notification);      btnCancel.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {     mNotification.cancel(ID); //--->取消通知    }   });  }   } 

6、NotificationReceiver.java源碼如下:

package com.andyidea.notification;  import com.andyidea.notification.SecondActivity;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;  public class NotificationReceiver extends BroadcastReceiver {   @Override  public void onReceive(Context context, Intent intent) {   //實例化Intent   Intent i = new Intent();   //在新任務中啟動Activity   i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   //設置Intent啟動的組件名稱   i.setClass(context, SecondActivity.class);   //啟動Activity,顯示通知   context.startActivity(i);  }  } 

7、程序運行效果如下:

以上就是針對Android中Notification使用方法進行的詳細介紹,希望對大家的學習有所啟發,幫助大家更好地學習Android軟件編程。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江阴市| 南城县| 多伦县| 南部县| 湖北省| 即墨市| 利川市| 仪征市| 介休市| 南充市| 尼勒克县| 平山县| 尼勒克县| 松溪县| 工布江达县| 含山县| 江津市| 大方县| 馆陶县| 淳化县| 黄陵县| 雷州市| 永城市| 浠水县| 和平区| 汽车| 丘北县| 蓝山县| 收藏| 肇东市| 新泰市| 水富县| 乃东县| 绵竹市| 伊金霍洛旗| 金山区| 金溪县| 和龙市| 公主岭市| 台北市| 吉水县|