android应用开发揭秘之Notification的简单用法

在android设备的顶端,会有消息提示栏,被称作notification , 有点像短消息
[界面]
图1 主界面:
android应用开发揭秘之Notification的简单用法 - huasoft - 快乐的机器猫 小桥加加网易分站
 

图2 点击按钮之后的反应:
android应用开发揭秘之Notification的简单用法 - huasoft - 快乐的机器猫 小桥加加网易分站
 

图3 拉下消息提示框的显示效果:
android应用开发揭秘之Notification的简单用法 - huasoft - 快乐的机器猫 小桥加加网易分站
 

[代码]
//------------ main.xml(与mynotfiy.java配合) -------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/mytextview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
    android:id="@+id/mybutton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="show..."
/>
</LinearLayout>

//------------ main2.xml (与desactivity.java配合)-----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="this is the Activity: DesActivity"
  />
</LinearLayout>


//---------- mynotfiy.java ------------
package com.stephenzhu.mynotify;

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.widget.Button;
import android.widget.TextView;

public class MyNotify extends Activity {
    Button mybutton1;
    TextView mytextview1;
   
    NotificationManager myNM;
    Notification myN;
   
    Intent myIntent;
    PendingIntent myPendingIntent;

   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
         myNM = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
        mybutton1 = (Button)this.findViewById(R.id.mybutton1);
       
        //点击通知时转移内容
        myIntent = new Intent(MyNotify.this, DesActivity.class);
        myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
        myN = new Notification();

       
        mybutton1.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 myN.icon = R.drawable.icon;
                myN.tickerText = "美美西饼店  有新活动哦";
                myN.defaults = Notification.DEFAULT_SOUND;
                myN.setLatestEventInfo(MyNotify.this, "有好吃的啦", "本店圣代买一送一,活动地址:漳州二路N号", myPendingIntent);
                myNM.notify(0, myN);

            }
           
        });
     }
}

//--------- desactivity.java ------------
package com.stephenzhu.mynotify;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class DesActivity extends Activity {
     NotificationManager myNM
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
         super.onCreate(savedInstanceState);         
         this.setContentView(R.layout.main2); 
          
         //启动后删除之前我们定义的 
         myNM=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); 
         myNM.cancel(0); 
        
    }     
}
//==========================================================

整个工程的包我存在自己的电子邮箱了,有想要的email我
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值