0

以下示例代码来自互联网,我不知道如何启动或停止监视器。我想在我安装 .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);  
 }  

 }  
4

3 回答 3

1

或者,如果您已经实施了上述解决方案,则可以像在 BroadcastReceiver 中检查 SharedPreference 以检查它是否应该转发 SMS 一样简单。

于 2013-09-25T02:55:07.577 回答
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 来检查是否应该转发消息。

于 2013-09-25T02:59:28.090 回答
0

使用ContentObserver代替BroadcastReceiver

getContentResolver().registerContentObserver(
            Uri.parse("content://mms-sms/"), true,     
ClassExtendingContentObserver);
于 2013-09-25T02:52:18.397 回答