状态栏的图标与文字提醒(NotificationManager与Notification对象的应用)
新建一个继承Activity类的NotificationManagerAndNotificationActivity,并设置布局文件为:notificationmanagerandnotification.xml。
布局文件很简单,就是一个Button按钮。
<Button android:id="@+id/notificationmanagerandnotification_btn" style="@android:style/Widget.Button.Inset" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/send_notification" /> |
而后是在Activity中,实现的功能是:单按钮点击的时候,发送一个Notification消息。
package lyx.feng.third;
import lyx.feng.simpletextdemo.R; 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.view.View.OnClickListener; import android.widget.Button;
public class NotificationManagerAndNotificationActivity extends Activity { private Button btn = null; private NotificationManager manager = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.notificationmanagerandnotification); this.btn = (Button) super .findViewById(R.id.notificationmanagerandnotification_btn); this.manager = (NotificationManager) super .getSystemService(NOTIFICATION_SERVICE); this.btn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { showNotification(); }
}); }
@SuppressWarnings("deprecation") private void showNotification() { Intent intent = new Intent(this, NotificationManagerAndNotificationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new Notification(); notification.icon = R.drawable.icon_004; notification.tickerText = "这是标题文字"; notification.defaults = Notification.DEFAULT_SOUND; notification. pendingIntent); manager.notify(0, notification); } }
|