以下示例代码来自互联网,我不知道如何启动或停止监视器。我想在我安装 .apk 后它会一直监控和转发短信。我希望我可以控制启动或停止监视器。我能怎么做?谢谢!
还有,我希望我的安卓手机开机自动启动显示器,怎么办?我需要使用本地服务器功能吗?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zizhu.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".BroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<!-- <uses-permission android:name="android.permission.INTERNET" /> -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>
package com.zizhu.broadcast;
import java.util.Date;
import java.text.SimpleDateFormat;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.widget.Toast;
public class BroadcastReceiver extends android.content.BroadcastReceiver {
private static final String FROM = "";
private static final String TO = "";
public static final String TAG = "BroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Object[] puds = (Object[])intent.getExtras().get("pdus");
for(Object pud : puds){
byte[] bytes = (byte[])pud;
SmsMessage message = SmsMessage.createFromPdu(bytes);
Log.d(TAG, "content:" + message.getMessageBody());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.d(TAG, "time:" + sdf.format(new Date(message.getTimestampMillis())));
Log.d(TAG, "sender:" + message.getOriginatingAddress());
// Toast.makeText(context, message.getMessageBody(), 1).show();
if(message.getOriginatingAddress().equals(FROM)){
sendMessage(message.getMessageBody(), TO);
}
}
}
private void sendMessage(String content, String to) {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(to, null, content, null, null);
}
}