- 【实训内容和步骤】
实训步骤:
haha
Settings
Hello world!
发送广播
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
运行结果截图:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)