Android 3新特性+源码实现(3)


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class NotificationActvity extends Activity{


 private NotificationManager nm = null;
 private Notification.Builder build = null;
 private Notification n;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.notifactions);
  findViewById();
 }
 
 private void findViewById(){
  Button notifications = (Button) findViewById(R.id.notifications);
  nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  build = new Notification.Builder(NotificationActvity.this);
  build.setDefaults(Notification.DEFAULT_SOUND);
  build.setAutoCancel(true);
  build.setContentTitle("title");
  build.setContentText("this is for message!");
  InputStream is = getResources().openRawResource(R.drawable.icon);
  Bitmap bitmap = new BitmapDrawable(is).getBitmap();
  build.setLargeIcon(bitmap);
//  build.setSmallIcon(R.drawable.icon);
  Intent it = new Intent(this,PopMenuActivity.class);
  PendingIntent pIntent = PendingIntent.getActivity(this, 0,it,0);
  build.setContentIntent(pIntent);
  n = build.getNotification();
  notifications.setOnClickListener(listener);
 }
 
 private OnClickListener listener = new OnClickListener() {
  @Overridepackage com.jsd.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;


/**
 *
 * @author jankey
 *
 */
public class PopMenuActivity extends Activity{


 PopupMenu popup = null;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.popumenu);
  findViewById();
 }
 
 private void findViewById(){
 }
 
 public void onPopuButtonClick(View button){
  popup = new PopupMenu(this, button);
  popup.getMenuInflater().inflate(R.menu.popu, popup.getMenu());
  popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
   @Override
   public boolean onMenuItemClick(MenuItem item) {
    if(item.getTitle().equals("exit")){
     popup.dismiss();
    }else{
     Toast.makeText(PopMenuActivity.this, item.getTitle(), 1).show();
    }
    return true;
   }
  });
  popup.show();
 }
}
  public void onClick(View v) {
   nm.notify(0, n);
   Toast.makeText(NotificationActvity.this,"notifications..",0).show();
  }
 };
}

package com.jsd.demo;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwgsws.html