public class MainActivity extends Activity {
Button btn_open;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_open = (Button) this.findViewById(R.id.button1);
btn_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showNotification();
//先啟動再finsih
finish();
}
});
}
@SuppressWarnings("deprecation")
public void showNotification() {
NotificationManager barManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
//取得NotificationManager型態物件
//標題顯示抬頭
@SuppressWarnings("deprecation")
Notification bMsg = new Notification(
android.R.drawable.stat_notify_sync
//顯示的圖標
, "通知測試----Title1",
//顯示捲動的訊息
System.currentTimeMillis()
//抓取當時的時間戳記
);
//初始化一個Notification物件
//拉桿窗簾
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
//如果選擇此通知,則以PendingIntent 推出 Activity
bMsg.setLatestEventInfo(MainActivity.this, "通知欄---Title2",
"通知測試----content", contentIntent);
barManager.notify(0, bMsg);
//執行以MainActivity之Activity發出通知
}
}
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="開啟通知" />
</RelativeLayout>
留言列表