项目中期(代码阶段)

项目中期


周末闲着无聊,就自学了markdown,不太娴熟,希望以后慢慢进步。

上个周末我把我们项目的安卓端的注册和登录界面实现了,然后自己把安卓的广播方面看了下,能实现双发发送信息了,原理就是调用系统的发送与接受消息的权限。

分为两步实现:

接受消息
发送消息

接受消息

当手机接受到一条消息的时候,系统会发出广播,这条广播携带者信息相关的数据。
先设计一组文字from和content表示来自某某和信息的内容的界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        //显示文字from
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="10dp"
            android:text="from:" /> 

        //显示文字接受框
        <TextView
            android:id="@+id/sender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" /> 
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        //显示文字content
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="10dp"
            android:text="Content:" /> 
        
        //显示接受框
        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" /> 
    </LinearLayout>

这只是页面布局,当然还要在main里面进行创建广播接收系统接受系统发出的广播。在清单文件里注册。

在模拟器能进行端进行模拟来消息

发送消息

再次在界面中加入文字输入框和send按钮

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="10dp"
            android:text="to:" />

        <EditText
            android:id="@+id/to"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        <EditText
            android:id="@+id/msg_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Send" />
    </LinearLayout>

再次在main文件中加入相应事件,再在清单中注册。就可以实现发送短信的功能。

小结:感觉学习一门新技术主要是要静下来看看书,然后不断上手实践代码效果。感觉这周最大的收获就是实现手机端的通信。了解了许多安卓的原理,关于布局,通信,活动的衔接。然后尽量多去掌握一些工具性质的东西,一开始感觉markdown应该会比较难学吧,但是由于没什么事,然后自己就试着学了下,感觉并不难,看了几篇文档也算小小的入个门。这里给个链接markdown入门我自己看了然后自己实践了一下子,感觉挺简单的,不用再进行调字体格式啥的,挺方便的。

接下来这是三个文件的总代码:
first.main文件

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class First extends Activity {
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first_layout);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(First.this,MainActivity.class);
				startActivity(intent);
			}

		});

	}
}

MainActivity.main文件

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	private TextView sender;
	private EditText to;
	private EditText msgInput;

	private Button send;

	private TextView content;

	private IntentFilter receiveFilter;
	private IntentFilter sendFilter;
	private MesseagReceiver messageReceiver;
	private SendStatusReceiver sendStatusReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sender = (TextView) findViewById(R.id.sender);
		content = (TextView) findViewById(R.id.content);
		receiveFilter = new IntentFilter();
		receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
		messageReceiver = new MesseagReceiver();
		registerReceiver(messageReceiver, receiveFilter);
		to = (EditText) findViewById(R.id.to);
		msgInput = (EditText) findViewById(R.id.msg_input);
		send = (Button) findViewById(R.id.send);

		sendFilter = new IntentFilter();
		sendFilter.addAction("SENT_SMS_ACTION");
		registerReceiver((BroadcastReceiver) sendStatusReceiver, sendFilter);
		send.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), null, null);
				Intent sentIntent = new Intent("SENT_SMS_ACTION");
				
				PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0);
				smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), pi, null);
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(messageReceiver);
		unregisterReceiver((BroadcastReceiver) sendStatusReceiver);
	}

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

	@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();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class MesseagReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			Object[] pdus = (Object[]) bundle.get("pdus");
			SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < messages.length; i++) {
				messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			}
			String address = messages[0].getOriginatingAddress();

			String fullMessage = "";

			for (SmsMessage message : messages) {
				fullMessage += message.getMessageBody();
			}
			sender.setText(address);
			content.setText(fullMessage);

		}
	}

	class SendStatusReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (getResultCode() == RESULT_OK) {
				Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG).show();
				
			} else {
				Toast.makeText(context, "Send failed", Toast.LENGTH_LONG).show();
		
			}
		}
	}
}

刚刚的接受发信息界面代码已经给出,这是前面的登陆与注册界面代码:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1" >

    <TableRow>

        <TextView
            android:layout_height="wrap_content"
            android:text="Account" />

        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content"
            android:hint="Input your account" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_height="wrap_content"
            android:text="Password" />

        <EditText
            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="register" />

        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
          
            android:text="login" />
    </TableRow>

</TableLayout>

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smstest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".First"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" >
        </activity>
    </application>

</manifest>
posted @ 2016-04-12 00:24  111231231241  阅读(264)  评论(0编辑  收藏  举报