Android学习第十五天----notification应用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="50dp"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="70dp"
        android:layout_toRightOf="@+id/button1"
        android:text="Button2" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="50dp"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

l通过一个按钮点击,然后在在通知栏显示通知信息;

package cn.core.test;

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 MainActivity extends Activity implements OnClickListener {

    private Button button1 = null, button2 = null;
    private NotificationManager mNotificationManager = null;
             
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        
        button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if(v==button1)
        {
//            Notification mNotification = new Notification();
//            mNotification.icon=;
//            mNotification.tickerText=;
//            mNotification.when=;
//            
            
            Notification mNotification = new Notification(
                    android.R.drawable.presence_online, "通知",
                    System.currentTimeMillis());
    
            mNotification.defaults = Notification.DEFAULT_VIBRATE;
    
            Intent intent = new Intent(MainActivity.this, Activity01.class);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    intent, 0);
    
            mNotification.setLatestEventInfo(this, "开会通知11", "通知您下午17:60分在卫生间开会",
                    pendingIntent);
    
            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            mNotificationManager.notify(11111, mNotification);
        }
        else if(v==button2)
        {
            mNotificationManager.cancelAll();
            
        }
        
        
        
        
        
        
        
    }

}

 

posted @ 2013-03-25 22:53  小三小山  阅读(155)  评论(0编辑  收藏  举报