ITeye博客

android notification的用法

memewry · 2012-03-18 11:53 · Markdown

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://jackxlee.blog.51cto.com/2493058/682435

Notification和NotificationManager的操作相对比较简单,一般用来获取系统级的服务NotificationManager,然后实例化Notification的对象,设置它的一系列属性(比如说图标、时间、标题、内容等),最后通过NotificationManager发出通知即可。

The description from SDK about Notification:


AclassthatrepresentshowapersistentnotificationistobepresentedtotheuserusingtheNotificationManager. 

TheNotification.BuilderhasbeenaddedtomakeiteasiertoconstructNotifications. 

Foraguidetocreatingnotifications,seetheCreatingStatusBarNotificationsdocumentintheDevGuide.

接下来使用一个简单的案例来演示一下Notification的使用:

首先MainActivity的代码如下:


publicclassMainActivityextendsActivity{ 
privateButtonbtnSend; 
 
//定义BroadcastReceiver的action 

privatestaticfinalStringNotificationDemo_Action="com.ceo.notification.activity.NotificationDemo_Action"; 

 
/**Calledwhentheactivityisfirstcreated.*/ 
@Override 
publicvoidonCreate(BundlesavedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
 
//getthewidgets'instance 
getInstance(); 
 
btnSend.setOnClickListener(newView.OnClickListener(){ 
@Override 
publicvoidonClick(Viewv){ 

Intentintent=newIntent(); 

intent.setAction(NotificationDemo_Action); 
sendBroadcast(intent); 
} 
}); 
} 
 
publicvoidgetInstance(){ 

btnSend=(Button)findViewById(R.id.btnSend); 

 
} 
}

相对应的main.xml布局:


<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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="Notification应用的小案例"


/>


<Button


android:id="@+id/btnSend"


android:text="sendnotification"


android:layout_width="wrap_content"


android:layout_height="wrap_content"


/> 


</LinearLayout>

SecondActivity的代码如下:


publicclassSecondActivityextendsActivity{ 
privateButtonbtnCancel; 
//声明Notification 
privateNotificationnotification; 
//声明NotificationManager 
privateNotificationManagermNotification; 
//标识Notification的ID 

privatestaticfinalintID=1; 

 
@Override 
protectedvoidonCreate(BundlesavedInstanceState){ 
//TODOAuto-generatedmethodstub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.second); 
 
getInstance(); 
//怎样获得NotificationManager的实例? 

Stringservice=NOTIFICATION_SERVICE; 


mNotification=(NotificationManager)getSystemService(service); 

 
//获得Notification的实例 

notification=newNotification(); 

 
//设置该图标会在状态栏显示 

inticon=notification.icon=android.R.drawable.stat_notify_chat; 

//设置提示信息 

StringtickerText="TestNotification"; 

//设置显示时间 

longwhen=System.currentTimeMillis(); 


notification.icon=icon; 


notification.tickerText=tickerText; 


notification.when=when; 

 

Intentintent=newIntent(this,MainActivity.class); 


PendingIntentpi=PendingIntent.getActivity(this,0,intent,0); 

notification.setLatestEventInfo(this,"消息","HelloAndroid",pi); 
mNotification.notify(ID,notification); 
 
btnCancel.setOnClickListener(newView.OnClickListener(){ 
@Override 
publicvoidonClick(Viewv){ 

mNotification.cancel(ID);//--->取消通知 

} 
}); 
} 
 
publicvoidgetInstance(){ 

btnCancel=(Button)findViewById(R.id.btnCancel); 

} 
 
}

相对应的second.xml布局:


<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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="cancelnotification"


android:layout_width="wrap_content"


android:layout_height="wrap_content"


/> 


</LinearLayout>

MyReceiver的代码如下:


publicclassMyReceiverextendsBroadcastReceiver{ 

@Override 
publicvoidonReceive(Contextcontext,Intentintent){ 
//实例化Intent 

Intenti=newIntent(); 

//在新任务中启动Activity 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//设置Intent启动的组件名称 
i.setClass(context,SecondActivity.class); 
//启动Activity,显示通知 
context.startActivity(i); 
} 

}

当然不要忘了在AndroidManifest文件中注册广播和Activity等:


<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"


package="com.ceo.notification.activity"


android:versionCode="1"


android:versionName="1.0">


<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">


<activityandroid:name=".MainActivity"


android:label="@string/app_name">


<intent-filter>


<actionandroid:name="android.intent.action.MAIN"/>


<categoryandroid:name="android.intent.category.LAUNCHER"/>


</intent-filter>


</activity>


<receiverandroid:name="MyReceiver">


<intent-filter>


<actionandroid:name="com.ceo.notification.activity.NotificationDemo_Action"/>


</intent-filter>


</receiver> 


<activityandroid:name=".SecondActivity"></activity>


</application>


<uses-sdkandroid:minSdkVersion="8"/>



</manifest>

最后直接上图:

至此Android中Notification的使用介绍完毕,预祝大家成功。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。