通知栏


public class ActivityMainNotification extends Activity {
    private static int NOTIFICATIONS_ID = R.layout.activity_notification; //当前页面的布局
    private NotificationManager mNotificationManager; //通知管理

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        Button button;

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //初始化管理器

        button = (Button) findViewById(R.id.sun_1);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setWeather("晴空万里", "天气预报", "晴空万里", R.drawable.sun);
            }
        });

        button = (Button) findViewById(R.id.cloudy_1);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setWeather("阴云密布", "天气预报", "阴云密布", R.drawable.cloudy);
            }
        });

        button = (Button) findViewById(R.id.rain_1);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setWeather("大雨连绵", "天气预报", "大雨连绵", R.drawable.rain);
            }
        });

        button = (Button) findViewById(R.id.defaultSound);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setDefault(Notification.DEFAULT_SOUND);
            }
        });

        button = (Button) findViewById(R.id.defaultVibrate);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setDefault(Notification.DEFAULT_VIBRATE);
            }
        });

        button = (Button) findViewById(R.id.defaultAll);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setDefault(Notification.DEFAULT_ALL);
            }
        });

        button = (Button) findViewById(R.id.clear);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mNotificationManager.cancel(NOTIFICATIONS_ID); // 关闭一个通知
            }
        });

    }

    private void setWeather(String tickerText, String title, String content,
            int drawable) {
        //建立一个通知实例,第一个参数是图片,第二个标题栏上显示的文字,第三个是时间
        Notification notification = new Notification(drawable, tickerText,
                System.currentTimeMillis());

       
        //当单击下拉下来的标题内容时候做什么,这里是跳转到主界面。这里和下面是一起的。
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ActivityMain.class), 0);
       
        //Title 是拉下来的标题,Content也是下拉后的内容显示
        notification.setLatestEventInfo(this, title, content, contentIntent);

        //显示这个通知
        mNotificationManager.notify(NOTIFICATIONS_ID, notification);
    }

    private void setDefault(int defaults) {

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ActivityMain.class), 0);

        String title = "天气预报";
        String content = "晴空万里";

        final Notification notification = new Notification(R.drawable.sun,
                content, System.currentTimeMillis());

        notification.setLatestEventInfo(this, title, content, contentIntent);

        notification.defaults = defaults; //这个是关键的用来说明是否有声音,振动等。

        mNotificationManager.notify(NOTIFICATIONS_ID, notification);
    }
}

 

 

提示框


public class ActivityToast extends Activity {

    OnClickListener listener1 = null;
    OnClickListener listener2 = null;
    Button button1;
    Button button2;
    private static int NOTIFICATIONS_ID = R.layout.activity_toast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener1 = new OnClickListener() {
            public void onClick(View v) {
                setTitle("短时间显示Toast");
                showToast(Toast.LENGTH_SHORT);

            }
        };
        listener2 = new OnClickListener() {
            public void onClick(View v) {
                setTitle("长时间显示Toast");
                showToast(Toast.LENGTH_LONG);
                showNotification();
            }
        };
        setContentView(R.layout.activity_toast);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(listener1);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(listener2);
    }

    protected void showToast(int type) {

        View view = inflateView(R.layout.toast);//显示布局,从XML构造出View

        TextView tv = (TextView) view.findViewById(R.id.content);//设置显示内容
        tv.setText("加入专业Android开发社区eoeAndroid.com,让你的应用开发能力迅速提高");

        Toast toast = new Toast(this);
        toast.setView(view);
        toast.setDuration(type); //显示长还是短
        toast.show(); //显示
    }

    private View inflateView(int resource) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return vi.inflate(resource, null);
    }
   
    // 这段就是可以同时显示标题栏的提示
    protected void showNotification() {

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //CharSequece 和String 类型一个意思
        CharSequence title = "最专业的Android应用开发社区";
        CharSequence contents = "eoeandroid.com";

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ActivityMain.class), 0);

        Notification notification = new Notification(R.drawable.default_icon,
                title, System.currentTimeMillis());

        notification.setLatestEventInfo(this, title, contents, contentIntent);

        // 100ms延迟后,振动250ms,停止100ms后振动500ms
        notification.vibrate = new long[] { 100, 250, 100, 500 };

        notificationManager.notify(NOTIFICATIONS_ID, notification);
    }
}

posted on 2011-05-13 17:53  冯瑞涛  阅读(3956)  评论(0编辑  收藏  举报