編輯:關於Android編程
notification是一種讓你的應用程序在沒有開啟情況下或在後台運行警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發生的最好途徑。
1、新建一個android項目
我新建項目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。
2、習慣性地打開項目清單文件AndroidManifest.xml,添加一個權限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的。
3、在布局activity_main.xml中添加幾個按鈕,樣子就大概這樣,垂直排版的LinearLayout
具體代碼
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3.0以前版本的notification,用新的吧"
android:onClick="click"
/>
<Button
android:id="@+id/btn_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大視圖文本通知"
android:onClick="click"
/>
<Button
android:id="@+id/btn_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大視圖圖片通知"
android:onClick="click"
/>
<Button
android:id="@+id/btn_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條通知"
android:onClick="click"
/>
</LinearLayout>
4、MainActivity中的代碼:
package com.xin.day__notificationdemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v.app.NotificationCompat;
import android.support.v.app.NotificationCompat.BigPictureStyle;
import android.support.v.app.NotificationCompat.BigTextStyle;
import android.support.v.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
//通知的唯一標識,在一個應用程序中不同的通知要區別開來
private static final int NO = x;
private static final int NO = x;
private static final int NO = x;
private static final int NO = x;
//進度條要用
private int progress = ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//click方法,和xml文件中的各個按鈕的onClick屬性的值要一致
public void click(View view) {
//創建NotificationManager
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//用switch語句控制四個控件
switch (view.getId()) {
case R.id.btn_: {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "有消息了。。。";
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, ".以前的通知", "試試而已", pendingIntent);
notification.when = System.currentTimeMillis();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.number = ;
notification.vibrate = new long[]{, };
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//大視圖文本通知
//創建消息構造器,在擴展包
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//設置當有消息是的提示,圖標和提示文字
builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
//需要樣式
BigTextStyle style = new BigTextStyle();
style.setBigContentTitle("上課通知");//通知的標題
style.bigText("今天下午要在綜B上jsp");//通知的文本內容
//大視圖文本具體內容
style.setSummaryText("這是正常的課程安排,請各位同學按時上課");
builder.setStyle(style);
//顯示消息到達的時間,這裡設置當前時間
builder.setWhen(System.currentTimeMillis());
//獲取一個通知對象
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//發送(顯示)通知
//notify()第一個參數id An identifier for this notification unique within your application
//get?意思說,這個通知在你的應用程序中唯一的標識符
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//大視圖圖片通知
NotificationCompat.Builder builderPic = new Builder(this);
builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪體育提醒");
//進行設置
BigPictureStyle pictureStyle = new BigPictureStyle();
pictureStyle.setBigContentTitle("新浪體育 快船VS騎士 ");
pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
pictureStyle.setSummaryText(" 快船VS騎士 天王山之戰!!!");//不要在意文字
//設置樣式
builderPic.setStyle(pictureStyle);
//設置顯示的時間
builderPic.setWhen(System.currentTimeMillis());
Notification notification = pictureStyle.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//進度條通知
final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("進度條通知");
builderProgress.setProgress(, progress, false);
final Notification notification = builderProgress.build();
//發送一個通知
manager.notify(NO, notification);
//創建一個計時器
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
Log.i("progress",progress+"");
while(progress <= ){
progress ++;
try {
Thread.sleep();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新進度條
builderProgress.setProgress(, progress, false);
//再次通知
manager.notify(NO, builderProgress.build());
}
//計時器退出
this.cancel();
//進度條退出
manager.cancel(NO);
return;//結束方法
}
}, );
}
break;
default:
break;
}
}
}
5、運行:我的虛擬機版本是4.0的(api19),按住通知左(右)滑動就可以讓通知小時了。
效果如下:

Android中通過反射來設置顯示時間
這個Toast的顯示在Android中的用途還是很大的,同時我們也知道toast顯示的時間是不可控的,我們只能修改他的顯示樣式和顯示的位置,雖然他提供了一個顯示時間的設置
Android開發之適配器模式初探
我們在開發Android的時候經常通過Adapter把數據和UI對象連接在一起,spinner、ListView之類的控件都可以用適配器來自定義其組建,使其更加豐富。適配
Android 適配器教程(一)
最近在開發安卓的過程中發現ListView無疑是一個經常使用的組件,而提到ListView又不得不提一下適配器了,我打算用幾個實例,由淺入深的向大家仔細講解一下適配器的使
Android四大組件之ContentProvider
ContentProvider的一個重要的特點就是它是可以使得某些數據可以被跨進程訪問,一般我們的數據庫是不可跨進程被訪問,因為數據庫一般的數據是屬於某個應用程序的,如果