From ec7feef5888d8b2d4a11fc3a7ce507707a75dd13 Mon Sep 17 00:00:00 2001
From: xinxin06 <952751382@qq.com>
Date: Thu, 16 Jun 2016 00:12:58 +0800
Subject: [PATCH] #91....#48 lab5....lab9
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
实验基本完成
---
.../Com1314080901106Activity.java | 70 +++++++++----------
.../Com1314080901106Receiver.java | 16 +++++
.../res/layout/activity_com1314080901106.xml | 52 +++++++-------
3 files changed, 77 insertions(+), 61 deletions(-)
create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901106/Com1314080901106Receiver.java
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 @@
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
+
+