通知java
setDefaults(): 這隻通知LED等、音樂、震動等android
setAutoCancle():點擊通知後,是否自動消失app
setContentTitle():通知內容標題ide
setContentText():通知內容ui
setSmailIcom():通知小圖標this
SetLargeIcon():通知大圖標url
setTick():通知狀態欄提示信息spa
setContentIntent():雙擊後運行的Intent線程
直接上例子,第一個簡單點,就一個提示信息,點擊後調用一個頁面3d
第二個顯示一個下載的提示信息,帶進度
例子1
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發送通知" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="關閉通知" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { NotificationManager nm; Button btn1; Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取系統的NotificationManager服務 nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(MainActivity.this,LoginActivity.class); //對intent進行包裝 PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //建立通知 Notification nf=new Notification.Builder(MainActivity.this) //是否自動關閉 .setAutoCancel(true) //通知的提示信息 .setTicker("有新消息") //小圖標 .setSmallIcon(R.drawable.ic_launcher) //通知標題 .setContentTitle("一條新的通知") //通知內容 .setContentText("有新的通知,點擊查看") //設置系統默認聲音,默認LED燈 //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS) //設置自定義聲音 //.setSound(sound) //何時啓動 .setWhen(System.currentTimeMillis()) //設置該通知要啓動的intent .setContentIntent(pi).build(); //發送 nm.notify(1234, nf); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //取消 nm.cancel(1234); } }); } }
例子2
一個下載升級包的例子,點擊開始下載,通知欄會增長一條現實進度的消息,下載完成了,點擊可安裝apk
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android1:id="@+id/button1" android1:layout_width="wrap_content" android1:layout_height="wrap_content" android1:text="開始下載" /> </LinearLayout>
自定義xml
view1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="XXX.apk" android:textAppearance="?android:attr/textAppearanceSmall" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_toLeftOf="@+id/textView3" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/progressBar1" android:text="100K/1024K" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/progressBar1" android:text="90%" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/progressBar1" android:layout_below="@+id/textView3" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity{ NotificationManager nm; RemoteViews rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(MainActivity.this,DownService.class); intent.putExtra("appName", "shenghuoriliwannianli_39"); intent.putExtra("appURL", "http://gdown.baidu.com/data/wisegame/2dcb384356f57ea2/shenghuoriliwannianli_39.apk"); startService(intent); } }); } }
DownService.java
public class DownService extends Service{ private String appName; private String appURL; private NotificationManager ni; private Notification nf; private RemoteViews rv; private File updateFile; @Override public IBinder onBind(Intent intent) { // TODO 自動生成的方法存根 return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { appName=intent.getExtras().getString("appName"); appURL=intent.getExtras().getString("appURL"); //升級目錄 File updateDir = new File(Environment.getExternalStorageDirectory()+ "/test/"); //升級文件 updateFile = new File(updateDir + "/" + appName + ".apk"); //判斷文件夾是否存在,不存在則建立 if(!updateDir.exists()){ updateDir.mkdir(); } //判斷升級文件是否存在,不存在則建立 if(!updateFile.exists()){ try { updateFile.createNewFile(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } //建立NotificationManager ni=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); //建立notification nf=new Notification(R.drawable.ic_launcher,"下載",System.currentTimeMillis()); //將此通知放到通知欄的"Ongoing"即"正在運行"組中 nf.flags |= Notification.FLAG_ONGOING_EVENT; //定義notification顯示 rv=new RemoteViews(getPackageName(),R.layout.view1); rv.setProgressBar(R.id.progressBar1, 100, 0, true); rv.setTextViewText(R.id.textView1, appName); rv.setTextViewText(R.id.textView3, "0%"); rv.setTextViewText(R.id.textView2, "0K/0K"); nf.contentView=rv; ni.notify(R.layout.view1, nf); //開始下載 new DownLoadThread().start(); return super.onStartCommand(intent, flags, startId); } //定義一個handler,用來刷新界面 Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { //刷新數據 if(msg.what==0x01){ int downloadSzie=msg.getData().getInt("downloadSzie"); int totalSize=msg.getData().getInt("totalSize"); //沒下載完 int pos=downloadSzie*100/totalSize; rv.setTextViewText(R.id.textView3, pos+"%"); rv.setTextViewText(R.id.textView2, (downloadSzie/1024)+"K/"+(totalSize/1024)+"K"); //下載完了 if(downloadSzie==totalSize){ rv.setTextViewText(R.id.textView4,"下載完成,點擊安裝"); Intent in=new Intent(Intent.ACTION_VIEW); String path=updateFile.getPath(); Uri uri=Uri.fromFile(updateFile); in.setDataAndType(uri, "application/vnd.android.package-archive"); PendingIntent pendingIntent = PendingIntent.getActivity(DownService.this, 0, in, 0); //修改標題類型,點擊一次後關閉 nf.flags = Notification.FLAG_AUTO_CANCEL; nf.contentIntent=pendingIntent; } nf.contentView=rv; ni.notify(R.layout.view1, nf); } } }; //定義一個下載線程 class DownLoadThread extends Thread { private int totalSize=0; private int downloadSzie=0; @Override public void run(){ try { URL url; HttpURLConnection http; url = new URL(appURL); http=(HttpURLConnection)url.openConnection(); //記錄上次百分比,主要是爲了減小界面刷新 int pos=0; //timeout 10秒 http.setConnectTimeout(10*1000); http.setReadTimeout(10*1000); //獲取文件大小 totalSize=http.getContentLength(); //成功 if(http.getResponseCode()==200){ InputStream in=http.getInputStream(); FileOutputStream out=new FileOutputStream(updateFile,false); byte[] buffer =new byte[1024]; int readSize=0; while ((readSize=in.read(buffer))!=-1){ //保存到文件 out.write(buffer,0,readSize); //總下載數 downloadSzie =downloadSzie +readSize; //刷新界面 if(pos!=(downloadSzie*100/totalSize)){ Message msg=new Message(); msg.what=0x01; Bundle b=new Bundle(); b.putInt("downloadSzie", downloadSzie); b.putInt("totalSize", totalSize); msg.setData(b); handler.sendMessage(msg); pos=downloadSzie*100/totalSize; } } if(http!=null){ http.disconnect(); } in.close(); out.close(); } } catch (Exception e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } }