首先介面的部分 ,選用LinearLayout,包含2個Button,一個來產生通知,另外一個來清除通知,XML 如下
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/createNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="產生通知"
/>
<Button
android:id="@+id/cleanNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除通知"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
MainActivity中
首先建設置一個NotificationManager
接著在MainActivity 中 使用Notification.Builder 來設置Notification 物件實例
myNotificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
接下來為Builder create PendingIntent
PendingIntent pi=PendingIntent.getActivity(this, 0, it, 0);
builder.setContentIntent(pi);
最後利用notify 方法傳送notification
myNotificationManager.notify(NOTIFICATION_ID, builder.getNotification())
取消通知時就用 cancel方法
myNotificationManager.cancel(NOTIFICATION_ID);
整段程式如下
package com.example.mynotificationexample;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private NotificationManager myNotificationManager;
private Button btnCreate,btnClear;
private static final int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCreate=
(Button) findViewById(R.id.createNotification);
btnCreate.setOnClickListener(l);
btnClear=(Button)findViewById(R.id.cleanNotification);
btnClear.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
myNotificationManager.cancel(NOTIFICATION_ID);
}
});
}
OnClickListener l=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GenerateNotification();
}
};
public void GenerateNotification()
{
myNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder=new Notification.Builder(this);
builder.setContentTitle("****這是通知抬頭****");
builder.setTicker("***這是通知****");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentText("這真的是通知");
Intent it=new Intent(this,MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(this
, 0, it, 0);
builder.setContentIntent(pi);
myNotificationManager.notify(NOTIFICATION_ID
, builder.getNotification());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

沒有留言:
張貼留言