Android:阅读收到的彩信

时间:2019-07-05 03:15:28

标签: android sms mms android-mms

因此,我在Android上处理消息的经验非常少,我试图弄清我在这里需要做的事情。我试图从群聊中读取短信,以检测某些文本,然后在遇到该文本时执行某些操作。截取奇异的SMS消息很容易,但是我无法在我的生命中检测到MMS消息。我发现了几种不同的文章和文章,以不同的方式来解决不同的问题,但这超出了我的理解,仍然无法正常工作。

那我该如何检测和阅读MMS消息(仅是文本,我不在乎任何其他媒体)?

此外,我想将所有这些都放在不断运行的后台服务中。我已经运行了该服务,但是一段时间后它会自行销毁,如何停止该服务并将其强制为后台服务?

这是我一直在使用SMS的代码,并且一直在尝试操纵它们以用于MMS。

MessageService.java

public class MessageService extends Service {
    public static int SERVICE_RUNNING = 0;

    private SMSReceiver smsReceiver;
    private IntentFilter intentFilter;

    @Override
    public void onCreate() {
        super.onCreate();
        SERVICE_RUNNING = 1;
        Log.i("MESSAGE SERVICE", "Service started");
        smsReceiver = new SMSReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.addAction("android.provider.Telephony.MMS_RECEIVED");
        intentFilter.addAction("android.provider.Telephony.WAP_PUSH_RECEIVED");
        registerReceiver(smsReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SERVICE_RUNNING = 0;
        unregisterReceiver(smsReceiver);
        Log.i("MESSAGE SERVICE", "Service killed");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("MESSAGE SERVICE", "Service bound?");
        return null;
    }

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

    private class SMSReceiver extends BroadcastReceiver {
        private final String TAG = this.getClass().getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "GOT A MESSAGE 1");
            Bundle extras = intent.getExtras();
            String strMessage = "";
            if(extras != null) {
                Object[] smsExtras =(Object[])extras.get("pdus");
                for(int i = 0; i < smsExtras.length; i++) {
                    SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])smsExtras[i]);
                    String msgBody = smsMsg.getMessageBody();
                    String msgSrc = smsMsg.getOriginatingAddress();
                    strMessage += "SMS from " + msgSrc + " : " + msgBody;
                    Log.i(TAG, strMessage);
                }
            }
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Intent serviceIntent;
    protected ServiceConnection mConn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("SEND MESSAGE", "Started activity");

        serviceIntent = new Intent(this, MessageService.class);
        mConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                Log.d("SEND MESSAGE", "onServiceConnected");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d("SEND MESSAGE", "onServiceDisconnected");
            }
        };

        // Create button onclick
        final Button startButton = (Button)findViewById(R.id.start_button);
        final Button stopButton = (Button)findViewById(R.id.stop_button);

        if(MessageService.SERVICE_RUNNING == 0) {
            stopButton.setEnabled(false);
        } else {
            startButton.setEnabled(false);
        }

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService();
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService();
                stopButton.setEnabled(false);
                startButton.setEnabled(true);
            }
        });
    }

    private void startService() {
        Log.i("SEND MESSAGE ACTIVITY", "TRYING TO START SERVICE");
        startService(serviceIntent);
    }

    private void stopService() {
        Log.i("SEND MESSAGE ACTIVITY", "TRYING TO STOP SERVICE");
        stopService(serviceIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(MessageService.SERVICE_RUNNING == 1)
            bindService(serviceIntent, mConn, Context.BIND_AUTO_CREATE);
    }
}

AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/app_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/app_icon_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">

    <meta-data
        android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc" />

    <activity android:name=".PermissionCheck">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />

    <service
        android:name=".MessageService"
        android:enabled="true">
    </service>
</application>

感谢您的帮助

0 个答案:

没有答案