How can I create a project in Android Studio to function default SMS app

前端 未结 1 2001
半阙折子戏
半阙折子戏 2021-01-14 19:46

Continuation of the previous query, see: SMS Receiver for AND API 19 and higher

I need to make the application run in the background and I could have it after instal

相关标签:
1条回答
  • 2021-01-14 20:05

    I am tried with this code it help me...

    manifests.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newsmsapp">
    
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/smsicon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.newsmsapp.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
    
        </activity>
    
        <activity android:name="com.example.newsmsapp.ComposeSMS">
    
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
    
    
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <category android:name="android.intent.category.LAUNCHER" />
    
                <data android:scheme="sms"/>
                <data android:scheme="smsto"/>
                <data android:scheme="mms"/>
                <data android:scheme="mmsto"/>
    
            </intent-filter>
        </activity>
    
        <receiver
            android:name="com.example.newsmsapp.SMSReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
    
    
            </intent-filter>
        </receiver>
    
        <activity
            android:name="com.example.newsmsapp.NotificationView"
            android:label="@string/title_activity_notification_view"
            android:theme="@style/AppTheme.NoActionBar">
    
        </activity>
    
        <receiver android:name="com.example.newsmsapp.MMSReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER"/>
                <data android:mimeType="application/vnd.wap.mms-message"/>
            </intent-filter>
        </receiver>
    
        <service android:name="com.example.newsmsapp.HandlessSMSSendService"
            android:exported="true"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
                <data android:scheme="sms"/>
                <data android:scheme="smsto"/>
                <data android:scheme="mms"/>
                <data android:scheme="mmsto"/>
            </intent-filter>
        </service>
    </application>
    

    then create 4 file for smsReader,mmsReader,ComposeSMS,HeadlessSMSservice

    smsReader.java

    public class SMSReceiver extends BroadcastReceiver {
    Notification notification;//=new Notification(R.drawable.icon,"New Message",System.currentTimeMillis());
    NotificationManager notificationmaneger;
    
    String SMSmsg;
    public SMSReceiver() {
    }
    public static final String SMS_BUNDLE = "pdus";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
    
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
    
                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();
    
                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
    
                builder.setAutoCancel(true);
                //  builder.setTicker("this is ticker text");
                builder.setContentTitle("New Messge");
                builder.setContentText(SMSmsg);
                builder.setSmallIcon(R.drawable.smsicon);
                builder.setColor(context.getResources().getColor(R.color.colorAccent));
                builder.setContentIntent(pendingIntent);
                builder.setOngoing(true);
                // builder.setSubText("You have pending tax");   //API level 16
                //builder.setNumber(100);
                builder.build();
            }
    SMSmsg=smsMessageStr;
    
        }
    }
    

    mmsReader.java

    public class MMSReceiver extends BroadcastReceiver
    {
        public MMSReceiver()
        {
         }
        @Override
        public void onReceive(Context context, Intent intent) {
        }
      }
    

    ComposeSMS.java

    public class ComposeSMS extends Activity {
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     }
    
    }
    

    HeadlessSMSservices.java

    public class HandlessSMSSendService extends Service {
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
        }
     }
    

    if u want to see SMS then add listview in composeSMS.java and bind received data from sms table to listview adapter.

    0 讨论(0)
提交回复
热议问题