最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android中Notification的使用方法

 更新時(shí)間:2015年12月24日 11:45:26   作者:cjjky  
這篇文章主要介紹了Android中Notification的使用方法,最典型的應(yīng)用就是未看短信和未接來電的顯示,還有QQ微信,想要深入了解Notification的朋友可以參考本文

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

Notification我把他分為兩種,一種是默認(rèn)的顯示方式,另一種是自定義的,今天為大家講述默認(rèn)的顯示方式
1、程序框架結(jié)構(gòu)圖如下


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實(shí)例" /> 
<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; 
 //標(biāo)識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的實(shí)例? 
  String service = NOTIFICATION_SERVICE; 
  mNotification = (NotificationManager)getSystemService(service); 
   
  //獲得Notification的實(shí)例 
  notification = new Notification(); 
   
  //設(shè)置該圖標(biāo) 會在狀態(tài)欄顯示 
  int icon = notification.icon = android.R.drawable.stat_sys_phone_call; 
  //設(shè)置提示信息 
  String tickerText = "Test Notification"; 
  //設(shè)置顯示時(shí)間 
  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) { 
  //實(shí)例化Intent 
  Intent i = new Intent(); 
  //在新任務(wù)中啟動(dòng)Activity 
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  //設(shè)置Intent啟動(dòng)的組件名稱 
  i.setClass(context, SecondActivity.class); 
  //啟動(dòng)Activity,顯示通知 
  context.startActivity(i); 
 } 
 
} 

7、程序運(yùn)行效果如下:

以上就是針對Android中Notification使用方法進(jìn)行的詳細(xì)介紹,希望對大家的學(xué)習(xí)有所啟發(fā),幫助大家更好地學(xué)習(xí)Android軟件編程。

相關(guān)文章

  • android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)

    android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)

    這篇文章主要為大家詳細(xì)介紹了android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    這篇文章主要介紹了Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法,結(jié)合實(shí)例形式分析了Android頁面之間進(jìn)行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android開發(fā)中TextView各種常見使用方法小結(jié)

    Android開發(fā)中TextView各種常見使用方法小結(jié)

    這篇文章主要介紹了Android開發(fā)中TextView各種常見使用方法,結(jié)合實(shí)例形式總結(jié)分析了Android開發(fā)中TextView各種常見布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-04-04
  • adb無法連接雷電模擬器問題的解決方式

    adb無法連接雷電模擬器問題的解決方式

    雷電模擬器優(yōu)點(diǎn)是可設(shè)置的選項(xiàng)要比天天模擬器多,下面這篇文章主要給大家介紹了關(guān)于adb無法連接雷電模擬器問題的解決方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Android集成zxing掃碼框架功能

    Android集成zxing掃碼框架功能

    這篇文章主要介紹了Android集成zxing掃碼框架功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Android 手動(dòng)獲取判斷處理權(quán)限

    Android 手動(dòng)獲取判斷處理權(quán)限

    本篇文章主要介紹了Android手動(dòng)獲取判斷處理權(quán)限的方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-05-05
  • Android性能優(yōu)化死鎖監(jiān)控知識點(diǎn)詳解

    Android性能優(yōu)化死鎖監(jiān)控知識點(diǎn)詳解

    這篇文章主要為大家介紹了Android性能優(yōu)化死鎖監(jiān)控知識點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù)

    Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù)

    這篇文章主要介紹了Android簡單實(shí)現(xiàn) 緩存數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android采取ContentObserver方式自動(dòng)獲取驗(yàn)證碼

    Android采取ContentObserver方式自動(dòng)獲取驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了Android采取ContentObserver方式自動(dòng)獲取驗(yàn)證碼,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡

    Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡

    這篇文章主要介紹了Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評論

龙里县| 呈贡县| 治县。| 阳春市| 深州市| 泗水县| 南岸区| 略阳县| 乾安县| 奉贤区| 新民市| 鸡泽县| 时尚| 延吉市| 轮台县| 两当县| 双江| 锡林浩特市| 青岛市| 百色市| 宁安市| 邢台市| 昭通市| 尼玛县| 安庆市| 平潭县| 绍兴市| 东港市| 永兴县| 长岭县| 宁阳县| 体育| 翁源县| 阿克| 榆树市| 兴文县| 河津市| 云和县| 恭城| 福州市| 梁平县|