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

詳解Android中Notification的使用方法

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

      在消息通知的時候,我們經(jīng)常用到兩個控件Notification和Toast。特別是重要的和需要長時間顯示的信息,用Notification最合適不過了。他可以在頂部顯示一個圖標(biāo)以標(biāo)示有了新的通知,當(dāng)我們拉下通知欄的時候,可以看到詳細(xì)的通知內(nèi)容。
      最典型的應(yīng)用就是未看短信和未接來電的顯示,還有QQ微信,我們一看就知道有一個未接來電或者未看短信,收到QQ離線信息。同樣,我們也可以自定義一個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實例" /> 
<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的實例? 
  String service = NOTIFICATION_SERVICE; 
  mNotification = (NotificationManager)getSystemService(service); 
   
  //獲得Notification的實例 
  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è)置顯示時間 
  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(); 
  //在新任務(wù)中啟動Activity 
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  //設(shè)置Intent啟動的組件名稱 
  i.setClass(context, SecondActivity.class); 
  //啟動Activity,顯示通知 
  context.startActivity(i); 
 } 
 
} 

7、程序運行效果如下:

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

相關(guān)文章

  • android studio開發(fā)實現(xiàn)APP開機自啟動

    android studio開發(fā)實現(xiàn)APP開機自啟動

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

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

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

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

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

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

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

    Android集成zxing掃碼框架功能

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

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

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

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

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

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

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

    Android采取ContentObserver方式自動獲取驗證碼

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

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

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

最新評論

盐城市| 咸宁市| 宜城市| 长春市| 弋阳县| 余干县| 广南县| 民丰县| 双鸭山市| 攀枝花市| 唐山市| 临武县| 翁牛特旗| 德钦县| 深水埗区| 紫阳县| 茶陵县| 黄石市| 郁南县| 伽师县| 儋州市| 上思县| 洪湖市| 靖安县| 开原市| 泰顺县| 武功县| 新宾| 清镇市| 高阳县| 兖州市| 闻喜县| 大冶市| 淅川县| 南充市| 临夏市| 永登县| 武平县| 承德市| 阳曲县| 屯留县|