android案例之有序广播

1.首先布局页面,代码如下:

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_order"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/stitch_one"
    tools:context="cn.edu.bzu.broadcast.OrderActivity">
    

 
android案例之有序广播_第1张图片

2.编写主程序代码

package cn.edu.bzu.broadcast;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_ipnumber;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ipnumber= (EditText) findViewById(R.id.et_ipnumber);
        sp=getSharedPreferences("config",MODE_PRIVATE);
    }
    public void click(View view){
        String number=et_ipnumber.getText().toString().trim();
        SharedPreferences.Editor editor = sp.edit();//得到编辑器
        editor.putString("number",number);
        editor.commit();
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }
}
3.定义广播接受者

package cn.edu.bzu.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class FirstReceiver extends BroadcastReceiver {
    public FirstReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("FirstReceiver","自定义广播接受者one,接收到广播事件");
    }
package cn.edu.bzu.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class TwoReceiver extends BroadcastReceiver {
    public TwoReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("TwoReceiver","自定义广播接受者two,接收到广播事件");
        abortBroadcast();
        Log.i("TwoReceiver","我的广播接受者two,广播被我终结了");
    }
package cn.edu.bzu.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ThreeReceiver extends BroadcastReceiver {
    public ThreeReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("ThreeReceiver","自定义广播接受者three,接收到广播事件");
    }
}
android案例之有序广播_第2张图片

你可能感兴趣的:(android案例之有序广播)