Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Notification 詳細運用

Notification 詳細運用

編輯:關於Android編程

package com.example.noti;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends
Activity {


@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
//NotificationManager獲取NOTIFICATION_SERVICE

final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
findViewById(R.id.button1)
.setOnClickListener(
new OnClickListener() {


@SuppressWarnings("deprecation")
public void onClick(
View v) {
// TODO Auto-generated method stub

//創建一個Notification

Notification notification = new Notification();
//狀態欄顯示信息


notification.icon = R.drawable.ic_launcher;


notification.tickerText = "This is test notification55555";
notification.vibrate = new long[] {
0,
100,
200,

300 };
//系統鈴聲

String ringname = RingtoneManager
.getActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE)
.toString();
notification.sound = Uri
.parse(ringname);
//閃光燈
notification.ledOnMS=300;
notification.ledOffMS=1000;

//切換到另一個ACTIVITY
PendingIntent pendingIntent = PendingIntent
.getActivity(
MainActivity.this,
0,
new Intent(
MainActivity.this,
MainActivity.class),
0);

//信息
notification
.setLatestEventInfo(
MainActivity.this,
"NOTIFICATION",
"This is test notification",
pendingIntent);
notificationManager
.notify(0,
notification);
}
});
}


@Override
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved