---
title: "制造垃圾短信"
author: "phenom"
date: "2013-01-28T14:25:33Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2013-01-28T14:34:01Z"
id: 1776753
url: "https://phenom.iteye.com/blog/1776753"
---

往系统中插入一条短信息，然后在通知栏中通知，点击通知栏后可以在系统短信列表中出现，就这样制造了一条垃圾短信了。

```java

虽然不知道是不是所有的机器都适合，但至少有成功的。
首选，往数据库中插入一条短信：
String message="message content";
        String fromAddress="130000";
        Uri uri=insertSms(fromAddress, message);

Uri insertSms(String fromAddress, String message) {
        ContentValues values=new ContentValues();
        values.put("address", fromAddress);
        values.put("body", message);
        //values.put("date", "20130121"); //不放时间就表示是当前的时间，
        values.put("read", 0);
        values.put("type", 1);
        //values.put("service_center", "+86161776500");

        Uri uri=getContentResolver().insert(mSmsUri, values);
        Log.d("", "uri:"+uri);
        return uri;
    }

接着制造通知：
Notification.Builder notification=new Notification.Builder(SmsSendIntentTestActivity.this)
            .setTicker(message)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(fromAddress)
            .setContentText(message)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.angel)
            .setContentIntent(createDisplayMessageIntent(SmsSendIntentTestActivity.this, fromAddress, message,
                notificationId, uri));

具体的通知创建：
private PendingIntent createDisplayMessageIntent(Context context, String fromAddress,
        String message, int notificationId, Uri uri) {

        Intent smsIntent=new Intent(Intent.ACTION_MAIN);//这里调用的是Main intent,如果是view就会到另一个显示界面中，暂时也不知道如何调用，把threadid查询到放进去，总是不成功，所以只到短信列表中。
        smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
        smsIntent.setType("vnd.android-dir/mms-sms"); 
        PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, smsIntent, 0);
        return pendingIntent;
    }

这样，一条垃圾短信就制造成功了，与系统短信不同的是，比如4.2.1系统通知栏会有回复，已读等按钮，这里就没有了。
还要自定义一个通知，把系统短信的图标拿来用就好了。

如果系统版较低，通知要修改下：
Intent smsIntent=new Intent(Intent.ACTION_MAIN);
        smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
        smsIntent.setType("vnd.android-dir/mms-sms");
        PendingIntent mPendingIntent=PendingIntent.getActivity(SmsSendIntentTestActivity.this, 0, smsIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

        mNotification.setLatestEventInfo(SmsSendIntentTestActivity.this, fromAddress, message, mPendingIntent);
        //发送通知
        notificationManager.notify(notificationId, mNotification);

```