Android中通知Notification使用实例(振动、灯光、声音)

2019-12-10 19:25:25王振洲

activity_main:

<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
 xmlns:tools="http://www.easck.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 
 <Button 
  android:id="@+id/send_notice" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="发出通知" 
  /> 
  
</LinearLayout> 

NotificationActivity:

import android.app.Activity; 
import android.app.NotificationManager; 
import android.os.Bundle; 
 
public class NotificationActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.notification_layout); 
 
  //打开NotificationActivity这个Activity后把通知给关掉 
  NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
  manager.cancel(1); 
 } 
 
} 

notification_layout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://www.easck.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
  
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_centerInParent="true" 
  android:textSize="24sp" 
  android:text="这是通知点击后的界面" 
  /> 
  
</RelativeLayout> 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://www.easck.com/apk/res/android" 
 package="com.example.notificationtest" 
 android:versionCode="1" 
 android:versionName="1.0" > 
 
 <uses-sdk 
  android:minSdkVersion="14" 
  android:targetSdkVersion="19" /> 
 
 <application 
  android:allowBackup="true" 
  android:icon="@drawable/ic_launcher" 
  android:label="@string/app_name" 
  android:theme="@style/AppTheme" > 
  <activity 
   android:name="com.example.notificationtest.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=".NotificationActivity" > 
  </activity> 
 
 </application> 
 
</manifest>