类似ios里面的通知~,直接上代码
MainActivity.java
1 package com.example.test; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 14 public class MainActivity extends Activity { 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 Button testBtn = (Button)findViewById(R.id.button1); 22 testBtn.setOnClickListener(new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 28 Intent intent = new Intent(); 29 intent.setAction("12345678");//这里给xml配置里面过滤用 30 intent.putExtra("asd", "dddddddddddddddddddddddddddddddddddddd"); 31 32 sendBroadcast(intent); 33 } 34 }); 35 } 36 }
MyReceiver.Java
1 package com.example.test; 2 3 import android.R.string; 4 import android.content.BroadcastReceiver; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.widget.Toast; 8 9 public class MyReceiver extends BroadcastReceiver { 10 11 public MyReceiver() { 12 // TODO Auto-generated constructor stub 13 } 14 15 @Override 16 public void onReceive(Context context, Intent intent) { 17 // TODO Auto-generated method stub 18 19 String info = intent.getStringExtra("asd"); 20 21 Toast.makeText(context, info, Toast.LENGTH_LONG).show(); 22 } 23 }
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.test" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="17" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.test.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <receiver android:name="MyReceiver"> 26 <intent-filter> 27 <action android:name="12345678"/> 28 </intent-filter> 29 </receiver> 30 </application> 31 32 </manifest>