zoukankan      html  css  js  c++  java
  • Notification通知栏

    Notification通知栏

    首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification

    是属于进程之前的通讯。进程之间的通讯是要在系统中获取系统的服务的。

    1.NotificationManager nm=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);

    接下就是把Notification类用NotificationManager来管理。

    2.nm.notify(0, notification);

    然后Notification还是设置内容,标题、图标、内容、跳转。

    代码功能:实现Notification中的通知栏打电话给110,别忘了Manifest.xml中的权限

      <uses-permission android:name="android.permission.CALL_PHONE"/>

    MainActivity.java

    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    
    	public void click(View view){
    		NotificationManager nm=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    		Notification notification=new Notification(R.drawable.ic_launcher, "我是一条消息", System.currentTimeMillis());
    		notification.flags=Notification.FLAG_AUTO_CANCEL;
    		Intent intent=new Intent();
    		intent.setAction(Intent.ACTION_CALL);
    		intent.setData(Uri.parse("tel:110"));
    		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    		notification.setLatestEventInfo(this, "标题", "内容", contentIntent);
    		nm.notify(0, notification);
    	}
    	
    	
    }
    

    XML中的文件

    activity_main.xml

    <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"
        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" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <Button 
            android:onClick="click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:layout_centerInParent="true"
            android:text="点击显示通知栏"
            />
        
        
        
    
    </RelativeLayout>
    
  • 相关阅读:
    16条很有用的Chrome浏览器命令
    通用测试用例
    Vue中@click、@click.stop和@click.prevet用法
    Vue事件的函数传参
    Vue事件的基本用法
    vue中v-on和v-bind的区别
    Vue中数据的双向绑定方法
    v-once用法
    v-clock、v-text、v-html和v-pre详解
    IDEA给已有项目添加maven依赖
  • 原文地址:https://www.cnblogs.com/childhooding/p/4334050.html
Copyright © 2011-2022 走看看