android顺序广播

1 发广播
Intent intent = new Intent("顺序广播的Action")
sendOrderedBroadcast(intent,null);
2 编写广播类(多个)
class R1 extends BroadcastReceiver{
Log.i("r1", "r1");
}
class R2 extends BroadcastReceiver{
Log.i("r2", "r2");
}
class R3 extends BroadcastReceiver{
Log.i("r3", "r3");
}
3 在onStart中注册顺序广播
r1 = new R1();
IntentFilter f1 = new IntentFilter("广播的Action")
在意图过滤器中设置级别  数字高的先执行
f1.setPriority(数字)
this.registerReceiver(r1, f1);
4 在onStop()中关闭注册
unregisterReceiver(r1);
源码:
package com.example.d08orderbroadcast;


import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


private R1 r1;
private R2 r2;
private IntentFilter if2;
private R3 r3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View v){
Intent intent = new Intent("AA");
sendOrderedBroadcast(intent, null);
}


@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;
}
class R1 extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r1", "r1");

}

}
class R2 extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r2", "r2");
this.abortBroadcast();

}

}
class R3 extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r3", "r4");

}

}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
r1 = new R1();
IntentFilter if1 = new IntentFilter("AA");
if1.setPriority(111);
this.registerReceiver(r1, if1);
r2 = new R2();
IntentFilter if2 = new IntentFilter("AA");
if2.setPriority(222);
this.registerReceiver(r2, if2);
r3 = new R3();
IntentFilter if3 = new IntentFilter("AA");
if3.setPriority(333);
this.registerReceiver(r3, if3);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(r1);
unregisterReceiver(r2);
unregisterReceiver(r3);
super.onStop();
}
}
布局:
<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" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="106dp"
        android:text="Button" 
        android:onClick="doClick"/>


</RelativeLayout>



@SuppressLint("NewApi") public class MainActivity extends Activity { SmsReceiver myReceiver; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { test(); } }); } public void test(){ Cursor cursor = null; String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this); Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, this.getPackageName()); startActivity(intent); try { cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "address", "read" }, "read = ? ", new String[] {"0" }, "date desc"); if (cursor != null) { ContentValues values = new ContentValues(); values.put("read", "1"); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Log.v("cky", "" + cursor.getInt(cursor.getColumnIndex("_id")) + " , " + cursor.getString(cursor.getColumnIndex("address"))); int res = getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=?", new String[] { "" + cursor.getInt(cursor.getColumnIndex("_id")) }); Log.i("cky","geng xin = "+res); } } intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); cursor = null; } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值