diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Activity.java index ee919185..5c62b82a 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Activity.java @@ -1,52 +1,52 @@ -package com.example.chenpeixin.com1314080901106; +package edu.hzuapps.androidworks.homeworks.com1314080901106; -import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; +import android.content.Intent; +import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import android.os.Bundle; import android.view.View; -import android.view.Menu; -import android.view.MenuItem; -public class Com1314080901106Activity extends AppCompatActivity { +public class Com1314080901106Activity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_com1314080901106); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - } - }); - } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_com1314080901106, menu); - return true; + findViewById(R.id.btnSendMsg).setOnClickListener(this); + findViewById(R.id.btnReg).setOnClickListener(this); + findViewById(R.id.btnunReg).setOnClickListener(this); } + + + @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; + public void onClick(View v) { + + switch (v.getId()) { + case R.id.btnSendMsg: + //Intent i =new Intent(this,MyReceiver.class); + Intent i =new Intent(Com1314080901106Receiver.ACTION); + i.putExtra("data","HzuUniversity"); + sendBroadcast(i); + break; + case R.id.btnReg: + if(receiver==null) { + receiver=new Com1314080901106Receiver(); + registerReceiver(receiver,new IntentFilter(Com1314080901106Receiver.ACTION)); + } + break; + case R.id.btnunReg: + if(receiver!=null) { + unregisterReceiver(receiver); + receiver =null; + } + break; + } - return super.onOptionsItemSelected(item); } + + private Com1314080901106Receiver receiver = null; } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Receiver.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Receiver.java new file mode 100644 index 00000000..df70d082 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Receiver.java @@ -0,0 +1,16 @@ +package edu.hzuapps.androidworks.homeworks.com1314080901106; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class Com1314080901106Receiver extends BroadcastReceiver { + public static final String ACTION ="edu.hzuapps.androidworks.homeworks.com1314080901106.intent.action.Com1314080901106Receiver"; + public Com1314080901106Receiver() { + } + + @Override + public void onReceive(Context context, Intent intent) { + System.out.println("接收到了消息,消息的内容是:"+intent.getStringExtra("data")); + } +} diff --git a/app/src/main/res/layout/activity_com1314080901106.xml b/app/src/main/res/layout/activity_com1314080901106.xml index 57bd6849..b25998f4 100644 --- a/app/src/main/res/layout/activity_com1314080901106.xml +++ b/app/src/main/res/layout/activity_com1314080901106.xml @@ -1,30 +1,30 @@ - + - +