如何在Android Studio中创建项目以使用默认SMS应用程序

时间:2016-07-07 14:22:24

标签: broadcastreceiver

继续上一个查询,请参阅:SMS Receiver for AND API 19 and higher

我需要让应用程序在后台运行,我可以在安装默认设置后将其设置为如下所示:http://android-developers.blogspot.cz/2013/10/getting-your-sms-apps-ready-for-kitkat.html

所以我问如何创建一个项目来显示“Super Duper SMS”列表,最终设置为默认值。

整个程序必须作为服务运行,不需要任何基本功能的屏幕接收短信,应该在核心android中注册

感谢您的任何建议

1 个答案:

答案 0 :(得分:3)

我尝试使用此代码帮助我...

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>

然后为smsReader,mmsReader,ComposeSMS,HeadlessSMSservice创建4个文件

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;
    }
 }

如果您想查看SMS,请在composeSMS.java中添加listview,并将接收到的数据从sms表绑定到listview适配器。