下面的示例code是INTERENT,我不知道如何启动或停止监视器。我想,这将是始终监视和转发短信后,我安装了.apk文件。我希望我可以控制启动或停止监视器。我能怎么做?谢谢!
和更多的,我希望能够自动启动监视器时,我在我的Android手机上电,我该怎么办?我需要使用本地服务器功能?
<?XML版本=1.0编码=UTF-8&GT?; <清单的xmlns:机器人=http://schemas.android.com/apk/res/android 包=com.zizhu.broadcast 安卓版code =1 机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =10/> <应用 机器人:图标=@绘制/ ic_launcher 机器人:标签=@字符串/ APP_NAME> <接收机器人:名字=广播接收器> &所述;意图滤光器> <作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/> &所述; /意图滤光器> < /接收器> < /用途> !< - <使用许可权的android:NAME =android.permission.INTERNET对/> - > <使用许可权的android:NAME =android.permission.RECEIVE_SMS/> <使用许可权的android:NAME =android.permission.SEND_SMS/> < /清单>包com.zizhu.broadcast;进口java.util.Date;进口java.text.SimpleDateFormat的;进口android.content.Context;进口android.content.Intent;进口android.telephony.SmsMessage;进口android.telephony.gsm.SmsManager;进口android.util.Log;进口android.widget.Toast;公共类广播接收器扩展android.content.BroadcastReceiver { FROM =私有静态最后弦乐; 私有静态最终字符串=; 公共静态最后的字符串标记=广播接收器; @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ [对象]聚氨酯分散体=(对象[])intent.getExtras()获得(的PDU); 对于(PUD对象:聚氨酯分散体){ 字节[]字节=(字节[])布丁; SmsMessage消息= SmsMessage.createFromPdu(字节); Log.d(TAG,内容:+ message.getMessageBody()); SimpleDateFormat的SDF =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS); Log.d(TAG,时间:+ sdf.format(新日期(message.getTimestampMillis()))); Log.d(TAG,发件人:+ message.getOriginatingAddress()); // Toast.makeText(上下文,message.getMessageBody(),1).show(); 如果(message.getOriginatingAddress()。等于(FROM)){ 的sendMessage(message.getMessageBody(),TO); } } } 私人无效的sendMessage(字符串内容,字符串){ SmsManager经理= SmsManager.getDefault(); manager.sendTextMessage(于零,内容,NULL,NULL); } }
解决方案
另外,如果你已经实施上述解决方案,它可以为你的广播接收器检查共享preference以检查它是否应该转发容易短信。
The following sample code is from interent, I don't know how to start or stop the monitor. I guess that it will be always monitor and forward SMS after I installed the .apk . I hope I can control to start or stop the monitor. How can I do? Thanks!
And more, I hope to start the monitor automatically when I power on my android mobile phone, how can I do? Need I use local server function?
<?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);
}
}
解决方案
Alternatively, if you already have implemented the above solution, it could be as easy as checking a SharedPreference in your BroadcastReceiver to check if it should forward the SMS.