Android实训七

Android实训七,第1张

实训7 BroadcastReceiver广播接收器类 一、【实训目的】 (1) BroadcastReceiver广播接收器类的实现步骤 (2) 有序广播的实际应用 (3)无序广播的应用
  • 【实训内容和步骤】
1、创建一个无序广播,广播发送器发送一个信息:“我是FM广播台”,广播接收器接收到信息,并用d出对话框显示接收到的信息。

实训步骤:



    

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
	private Button sendBroadcast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Intent intent=new Intent();
		intent.setAction("com.example.haha.MyBroadcastReceiver");
		sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
		sendBroadcast.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
			sendBroadcast(intent);
			}
		});
	}

	@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;
	}

}

MyBroadcastReceiver.java

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "我是FM广播台",
				Toast.LENGTH_LONG).show();
	}
}

AndroidMainfest.xml




    

    
        
            
                

                
            
        
        
            
                
            
        
    

运行结果截图:

2、创建一个有序广播,有1,2,3,4,5广播接收器,广播发送器发送一个信息:“我是有序广播”,按照顺序每个广播接收器接收到信息,并用d出对话框显示接收到的信息。

实训步骤:

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
	private Button sendBroadcast,sortedBroadcast;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Intent intent=new Intent();
		intent.setAction("com.example.ww.MyBroadcastReceiver");
		MyBroadcastReceiver myBroadcastReceiver=new MyBroadcastReceiver();
		IntentFilter intentFilter=new IntentFilter("com.example.ww.MyBroadcastReceiver");
		registerReceiver(myBroadcastReceiver, intentFilter);
		sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
		sortedBroadcast=(Button)findViewById(R.id.sortedBroadcast);
		sendBroadcast.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
			sendBroadcast(intent);
			}
		});
		sortedBroadcast.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			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;
	}
}

AndroidMainfest.xml




    

    
        
            
                

                
            
        
        
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
    

运行结果截图:

 

 附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考! 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/721140.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存