如何从收件箱中删除通过数据库发送的短信?

时间:2019-01-17 07:50:25

标签: android smsmanager

当用户单击按钮时,我已经开发了一个SMS应用程序,所有消息均发送到手机号码,并保存在DB中(此处代码运行完美)。现在,我要以编程方式从收件箱中删除这些SMS。请在这里帮助我。

这是我的Java文件

public class Code extends AppCompatActivity{
int position;
private static final String URL = "http://192.168.1.10/SMS/msg.php";
private String Phone, mesage;
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
ListView listView2;
ArrayList<String> list;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_code);
    button = findViewById(R.id.button2);
    listView2 = findViewById(R.id.listView);

    list = new ArrayList<>();
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>(){
                @Override
                public void onResponse(String response){
                   try{
                        JSONArray array = new JSONArray(response);
                        SmsManager smsManager = SmsManager.getDefault();

                        for (int i = 0; i < array.length(); i++){
                            JSONObject menu = array.getJSONObject(i);
                            Phone = menu.getString("phone");
                            mesage = menu.getString("message");
                            try{
                                smsManager.sendTextMessage(Phone, null, mesage, null, null);
                                list.add("" + Phone + "    .....Sent");} catch (Exception e) {
                                list.add("" + Phone + "    .....Failed");
                            }

                            ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(Code.this, android.R.layout.simple_spinner_item, list);
                            listView2.setAdapter(dataAdapter);
                            sendSMSMessage();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
              new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Code.this, error.toString(), Toast.LENGTH_SHORT).show();
                        }

                    });
            Volley.newRequestQueue(Code.this).add(request);
        }
    });
}

protected void sendSMSMessage() {

    //To check if you have a permission,
    // call the ContextCompat.checkSelfPermission() method.
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {

            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "PERMISSION GRANTED", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "NO PERMISSION GRANTED", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

private void deleteMessage(){

}
}

我的清单文件

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Code" />
</application>

1 个答案:

答案 0 :(得分:0)

您不能这样做。只有默认的SMS提供程序可以从内容存储库中删除SMS。您必须将自己注册为SMS内容提供者才能删除SMS(当用户在默认应用选项中将您的应用标记为默认SMS提供者时)。