和单片机最小系统类似,只是最基本的。
最终由两个页面,一个server组成。包括activity ,server,broadcast。。。贴图麻烦
默认主activity:
public class MainActivity extends Activity { //继承Activity
private Button bt1;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //设置界面
bt1=(Button) findViewById(R.id.button1);
tv=(TextView) findViewById(R.id.textView1);
bt1.setOnClickListener(new OnClickListener() { //设置按钮监听
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1=new Intent(MainActivity.this, Secend.class); //创建Intent并指定目标
startActivity(intent1); //跳转到第二个activity
Toast.makeText(MainActivity.this, "jkslkfjlsjfls", Toast.LENGTH_LONG).show(); //Toast输出
}
});
}
@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);
menu.add("aassdd"); //增加menu菜单
return true;
}
}
第二个activaty:
public class Secend extends Activity { //继承Activity
private Button bt2;
private TextView tv,tm,tm2;
private SeekBar sb;
private BroadcastReceiver barreceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secend); //设置界面
bt2=(Button) findViewById(R.id.button2);
tv=(TextView) findViewById(R.id.number123);
sb=(SeekBar) findViewById(R.id.seekBar1);
tm=(TextView) findViewById(R.id.timer);
tm2=(TextView) findViewById(R.id.timer2);
bt2.setOnClickListener(new OnClickListener() { //设置按钮监听
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent2=new Intent(Secend.this, Third.class);
//startActivity(intent2);
Intent intent123=new Intent("123456789"); //这三行 发送广播
intent123.putExtra("NUM", sb.getProgress());
sendBroadcast(intent123);
}
});
BroadcastReceiver barreceiver = new BarBroadcastReceiver(); //创建广播接收器对象
registerReceiver(barreceiver, new IntentFilter("123456789")); //注册广播接收器
Intent ServiceIntent2 =new Intent(this,MyTimer2.class); //启动服务
startService(ServiceIntent2);
}
class BarBroadcastReceiver extends BroadcastReceiver{ //实现接收器类
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int num=intent.getIntExtra("NUM", -1);
if (num !=-1 )tv.setText("number is:"+num);
int time2=intent.getIntExtra("TIME", -1);
if (time2 != -1)tm2.setText("time2:"+time2);
}
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Intent ServiceIntent2 =new Intent(this,MyTimer2.class);
stopService(ServiceIntent2); //停止服务
unregisterReceiver(barreceiver); //注销广播接收器
}
}
server代码:
public class MyTimer2 extends Service{ //继承服务
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
super.onCreate();
My_thread2 my_thread2=new My_thread2(); //启动线程
Thread thread2=new Thread(my_thread2);
thread2.start();
}
class My_thread2 implements Runnable //实现线程
{
int time =0;
@Override
public void run() { //线程run方法
// TODO Auto-generated method stub
while(time<100){
Intent intent =new Intent("123456789");
intent.putExtra("TIME", time);
sendBroadcast(intent);
time++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}