80,352
社区成员
发帖
与我相关
我的任务
分享
package com.example.bluetooth___watch;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
/* 当收到短信时,就会触发此方法 */
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// 产生一个Toast
Toast toast = Toast.makeText(context,
"短信内容: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
// 设置toast显示的位置
// toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 200);
// 显示该Toast
toast.show();
//调用显示短信内容的Activity
Intent in = new Intent();
in.setClass(context, MainActivity.class);
in.putExtra("message", smsMessage[0].getMessageBody());
context.startActivity(in);
}
}
package com.example.bluetooth___watch;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
String username ;
/* 取得默认的蓝牙适配器 */
private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
/* 请求打开蓝牙 */
private static final int REQUEST_ENABLE = 0x1;
/* 请求能够被搜索 */
private static final int REQUEST_DISCOVERABLE = 0x2;
Button Button1, Button2, Button3,Button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = (Button) findViewById(R.id.open_bluetooth);
Button2 = (Button) findViewById(R.id.Can_be_searched);
Button3 = (Button) findViewById(R.id.search_device);
Button4 = (Button) findViewById(R.id.Show_Message);
// 设置按钮1的事件监听
Button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 用户请求打开蓝牙
// Intent enabler = new
// Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enabler, REQUEST_ENABLE);
// 打开蓝牙
_bluetooth.enable();
}
});
// 设置按钮2的事件监听
Button2.setOnClickListener(new Button.OnClickListener() {
/* 使设备能够被搜索 */
public void onClick(View v)
{
Intent enabler = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enabler, REQUEST_DISCOVERABLE);
}
});
// 设置按钮3的事件监听
Button3.setOnClickListener(new Button.OnClickListener() {
/* 开始搜索 */
public void onClick(View v)
{
Intent enabler = new Intent(MainActivity.this,
DiscoveryActivity.class);
startActivity(enabler);
}
});
Bundle bu = this.getIntent().getExtras();
username = bu.getString("message");//获取短信内容
/*
// 设置按钮4的事件信息 */
Button4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
DisplayToast(username);
}
});
}
/* 显示Toast */
public void DisplayToast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}