以下示例代码来自interent,我不知道如何启动或停止监视器。 我想在我安装.apk之后它会一直监视和转发短信。 我希望我可以控制启动或停止显示器。我能怎么做?谢谢!
此外,我希望在启动Android手机时自动启动显示器,我该怎么办?我需要使用本地服务器功能吗?
<?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);
}
}
答案 0 :(得分:1)
或者,如果您已经实现了上述解决方案,则可以像检查BroadcastReceiver中的SharedPreference一样轻松检查它是否应转发SMS。
答案 1 :(得分:1)
添加到您的清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.android.syshelper.BC" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
广播接收器:
public class BC extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("BC", intent.getAction());
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")){
//start a service, do whatever you want to do at boot
} else if (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Boolean shouldForward = preferences.getBoolean("shouldForward", true);
if (shouldForward){
//Do your message recv, forwarding, etc.
}
}
}
}
与RSenApps说的一样,使用SharedPreferences检查是否应该转发消息。
答案 2 :(得分:0)
使用ContentObserver
代替BroadcastReceiver
getContentResolver().registerContentObserver(
Uri.parse("content://mms-sms/"), true,
ClassExtendingContentObserver);