zoukankan      html  css  js  c++  java
  • android学习笔记22——Notification

    Notification

    ==>

    Notification是显示在手机状态栏的消息,位于手机屏幕的最上方;

    一般显示手机当前网络、电池状态、时间等;

    Notification所代表的是一种全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

    应用程序可通过NotificationManager向系统发送全局通知;

    使用Notification发送Notification,操作步骤:

      1.调用getSystemService(NoTIFICATION_SERVICE)方法获取系统的NotificationManager服务;

      2.通过构造器创建一个Notification对象;

      3.为Notification设置各种属性;

      4.通过NotificationManager发送Notification

    实例:

    布局文件==》
    <?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" >
    
        <Button
            android:id="@+id/btnone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="one" />
    
        <Button
            android:id="@+id/btntwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="two" />
    
    </LinearLayout>
    
    <?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" >
    
        <Button
            android:id="@+id/btnone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="otehr" />
    
        <Button
            android:id="@+id/btntwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="other" />
    
    </LinearLayout>
    
    代码实现==》
    package com.example.mynotification;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity
    {
    	final int NotificationId = 1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		Button btnone = (Button) this.findViewById(R.id.btnone);
    		Button btntwo = (Button) this.findViewById(R.id.btntwo);
    
    		btnone.setOnClickListener(new OnClickListener()
    		{
    			@SuppressWarnings("deprecation")
    			@Override
    			public void onClick(View v)
    			{
    				Intent intent = new Intent(MainActivity.this, OtherActivity.class);
    				PendingIntent pintent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    				// 创建Notification
    				Notification notify = new Notification();
    				// 为Notification设置图标,该图标显示在状态栏
    				notify.icon = R.drawable.ele;
    				// 为Notification设置文本内容, 该文本显示在状态栏
    				notify.tickerText = "启动其他程序通知";
    				// 设置Notification发送时间
    				notify.when = System.currentTimeMillis();
    				// 为Notification设置默认声音、默认震动、默认闪关灯
    				notify.defaults = Notification.DEFAULT_SOUND;
    				// 设置事件信息
    				notify.setLatestEventInfo(MainActivity.this, "Notification通知", "Notification查看", pintent);
    				// 获取系统NotificationManager服务
    				NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    				manager.notify(NotificationId,notify);
    			}
    		});
    
    		btntwo.setOnClickListener(new OnClickListener()
    		{
    			@Override
    			public void onClick(View v)
    			{
    				NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    				manager.cancel(NotificationId);
    			}
    		});
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    
    package com.example.mynotification;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class OtherActivity extends Activity
    {
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.other);
    	}
    }
    
    权限设置、Activity添加==》
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mynotification"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
        <!-- 添加操作权限 -->
        <!-- 添加闪光灯操作权限 -->
        <uses-permission android:name="android.permission.FLASHLIGHT" />
        <!-- 添加操作振动器的操作权限 -->
        <uses-permission android:name="android.permission.VIBRATE" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.mynotification.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.mynotification.OtherActivity"
                android:label="other acitivity" />
        </application>
    
    </manifest>
    

    注意:

    以上程序中粗体内容为Notification设置各种属性,包括图标、标题、发送时间。

    也可以通过以下方式实现:Notification notify= new Notification(R.drawable.image,"启动Activity的通知",System.currentTimeMillis());

    以上程序,还通过defaults属性Notification设置了声音提示、震动提示、闪光灯等,该属性支持的属性如下所示:

      1.DEFAULT_SOUND:设置使用默认声音;

      2.DEFAULT_VIBRATE:设置使用默认震动;

      3.DEFAULT_LIGHTS:设置使用默认闪光灯;

      4.ALL:设置使用默认声音、震动、闪光灯;

    如果不想使用默认设置,也可使用代码进行设置:

      //设置自定义声音

      notify.sound=Uril.parse("file://sdcard//music.mp3");

      //设置自定义震动

      notify.vibrate = new Long[]{0,50,100,150};

      //设置闪光灯颜色为红色

      notify.ledARGB=Color.Red;

      //设置闪光灯多少毫秒后熄灭

      notify.ledoffms=800ms;

      //设置闪光灯多少毫秒后开启

      notify.ledOnms=800ms;

    注意:添加新的Activity需要在AndroidMainfest.xml添加对应配置,需要操作系统设置,也需要在AndroidMainfest.xml添加对应的权限配置才可正常使用。

    运行效果:

           

  • 相关阅读:
    稳扎稳打Silverlight(47) 4.0UI之操作剪切板, 隐式样式, CompositeTransform, 拖放外部文件到程序中
    返璞归真 asp.net mvc (9) asp.net mvc 3.0 新特性之 View(Razor)
    返璞归真 asp.net mvc (6) asp.net mvc 2.0 新特性
    稳扎稳打Silverlight(48) 4.0其它之打印, 动态绑定, 增强的导航系统, 杂七杂八
    精进不休 .NET 4.0 (9) ADO.NET Entity Framework 4.1 之 Code First
    稳扎稳打Silverlight(42) 4.0控件之Viewbox, RichTextBox
    稳扎稳打Silverlight(53) 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp
    稳扎稳打 Silverlight 4.0 系列文章索引
    稳扎稳打Silverlight(54) 4.0通信之对UDP协议的支持: 通过 UdpAnySourceMulticastClient 实现 ASM(Any Source Multicast),即“任意源多播”
    返璞归真 asp.net mvc (8) asp.net mvc 3.0 新特性之 Model
  • 原文地址:https://www.cnblogs.com/YYkun/p/5786637.html
Copyright © 2011-2022 走看看