短信类型TEXT或MMS或其他?

时间:2011-08-10 07:23:16

标签: android

如何在Android程序化盟友中捕获传入的MESSAGE类型,无论是Text还是MMS还是其他什么?

1 个答案:

答案 0 :(得分:0)

早些时候我来这里寻求解决方案,但没有得到。 现在我已经解决了,并决定自己发布答案。

如果您想知道消息类型是文本还是彩信?

创建一个我们将启动服务的活动


public class ListenSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //start the service to listen 
    startService(new Intent(getBaseContext(),ListenSMSService.class));

}}

创建一个服务,我们将使用内容Observer


public class ListenSMSService extends Service{
//Context globalContext;
ContentResolver contentResolver;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub      
    return null;
}

@Override
public void onCreate() {

    registerObserver();
}

public void registerObserver() {                

contentResolver = getContentResolver();                     
contentResolver.registerContentObserver(Uri.parse
          ("content://mms-sms/conversations/"), true, 
          new MyObserver(new Handler()));               
Log.v("Debug", " in registerObserver method.........");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;        
}

class MyObserver extends ContentObserver 
{
    public MyObserver(Handler handler) {
        super(handler);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onChange(boolean selfChange) {
     super.onChange(selfChange);

     Log.v("Current Locaion", "in onChange method....");

     final String[] projection = new String[]{"_id", "ct_t"};
     Uri uri = Uri.parse("content://mms-sms/conversations/");
     Cursor query = contentResolver.query(uri, projection, null, null, null);
     query.moveToFirst();

     String string = query.getString(query.getColumnIndex("ct_t"));
         if ("application/vnd.wap.multipart.related".equals(string)) 
             {
            // it's MMS             
            Log.v("Debug", "it's MMS");
         } 
             else 
            {
            // it's SMS
            Log.v("Debug", "it's SMS");             

        }
   }        

}

}


最后添加权限并在清单文件中进行服务输入


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.rdc"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />    

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.rdc.ListenSMSActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>    


 <service android:enabled="true" android:name="com.rdc.ListenSMSService">  
    <intent-filter>  
        <action android:name="com.kns.ListenSMSService">  
        </action>  
    </intent-filter>


</service>    
</application>            
</manifest>

您将在Logcat中获得消息类型(sms / mms)。