2.NotificationExample.java
package tw.nicky; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class NotificationExample extends Activity { private Button notifyButn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); notifyButn = (Button)findViewById(R.id.notifyButn); notifyButn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //取得Notification服務 NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); //設定當按下這個通知之後要執行的activity Intent notifyIntent = new Intent(NotificationExample.this,NotificationExample.class); notifyIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent appIntent=PendingIntent.getActivity(NotificationExample.this,0, notifyIntent,0); Notification notification = new Notification(); //設定出現在狀態列的圖示 notification.icon=R.drawable.icon; //顯示在狀態列的文字 notification.tickerText="notification on status bar."; //會有通知預設的鈴聲、振動、light notification.defaults=Notification.DEFAULT_ALL; //設定通知的標題、內容 notification.setLatestEventInfo(NotificationExample.this,"Title","content",appIntent); //送出Notification notificationManager.notify(0,notification); } }); } }3. main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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:text="@string/hello" /> <Button android:text="Notify" android:id="@+id/notifyButn" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tw.nicky" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NotificationExample" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE" /> </manifest>
5. 執行畫面
版大請問一下 那有關警示音要如何寫呢?
回覆刪除