Android的消息通知--Notification

MainActivity是项目应用的主类

/**
 * @author Freedom
 * @2014 October 19th
 * 
 */

public class MainActivity extends Activity {

	// 声明Notification(通知)的管理者
	private NotificationManager mNotifyMgr;
	// 声明Notification(通知)对象
	private Notification notification;
	// 消息的唯一标示id
	public static final int mNotificationId = 001;

	// 声明一个启动按钮、关闭按钮
	private Button startbtn, cancelbtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startbtn = (Button) findViewById(R.id.start);
		cancelbtn = (Button) findViewById(R.id.cancel);

		// 绑定startbtn按钮的onclick事件
		startbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 创建一个即将要执行的PendingIntent对象
				Intent resultIntent = new Intent(MainActivity.this,
						ResultActivity.class);
				PendingIntent resultPendingIntent = PendingIntent.getActivity(
						MainActivity.this, 0, resultIntent,
						PendingIntent.FLAG_UPDATE_CURRENT);

				// 建立所要创建的Notification的配置信息,并有notifyBuilder来保存。
				notification = new Notification.Builder(MainActivity.this)
				// 触摸之后,通知立即消失
						.setAutoCancel(true)
						// 显示的时间
						.setWhen(System.currentTimeMillis())
						// 设置通知的小图标
						.setSmallIcon(R.drawable.smallicon)
						// 设置状态栏显示的文本
						.setTicker("状态栏提示消息")
						// 设置通知的标题
						.setContentTitle("通知的标题!")
						// 设置通知的内容
						.setContentText("通知的内容!")
						// 设置声音(系统默认的)
						// .setDefaults(Notification.DEFAULT_SOUND)
						// 设置声音(自定义)
						.setSound(
								Uri.parse("android.resource://org.crazyit.ui/"
										+ R.raw.msg))
						// 设置跳转的activity
						.setContentIntent(resultPendingIntent).build();

				// 创建NotificationManager对象,并发布和管理所要创建的Notification
				mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				mNotifyMgr.notify(mNotificationId, notification);

			}
		});
		// 绑定cancelbtn按钮的onclick事件
		cancelbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				mNotifyMgr.cancel(mNotificationId);

			}
		});
	}


</pre><p><span style="font-family: 'Microsoft YaHei';"><span style="font-size:18px;"><strong>跳转之后的ResultActivity</strong></span></span></p><p></p><pre code_snippet_id="490135" snippet_file_name="blog_20141020_2_7484839" name="code" class="java"><span style="font-size:18px;"><strong>/**
 * 
 * @author Freedom
 * @2014 October 19th
 * 
 */
public class ResultActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		
	}

}</strong></span>

mainactivity的布局文件

<span style="font-size:18px;"><strong><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <Button 
       android:id="@+id/start"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/startbtntext"
       />
   
   <Button 
       android:id="@+id/cancel"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/cancelbtntext"
       />

</LinearLayout></strong></span>

resultactivity的布局文件

<span style="font-size:18px;"><strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<ImageView 
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/android"
    android:scaleType="fitXY"
    android:contentDescription="@string/desciption"
    
    />    

</LinearLayout>
</strong></span>

Manifest文件

<span style="font-size:18px;"><strong><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mylatestnotifiction"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mylatestnotifiction.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity 
            android:name="com.example.mylatestnotifiction.ResultActivity"
            android:label="第二个页面"
            ></activity>
    </application>

</manifest></strong></span>






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值