2015-02-17 93 views
13

我試圖用此應用程序向我的手機發送消息,但未使用網絡使用情況,但我的代碼無法使用。我跟着一些教程,檢查Android開發,我沒有發現任何東西(在我的logcat中,我沒有錯誤)。你能幫我找出我的問題嗎?Android - 嘗試發送虛假短信給自己,無需移動網絡使用

我對編譯器,編譯器和電話信息:

  • 的Android 1.0.1工作室

  • API 19的Android 4.4.4(KitKat)的

  • 構建19

  • Android手機版4.4.4

清單:

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

我的主要活動的功能:

Context context; 
String sender; 
String body; 

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

    //Get current context 
    context = this; 

    //App started 
    Toast.makeText(context, "Started", Toast.LENGTH_LONG).show(); 

    CheckApp(); 
} 

private void CheckApp() { 

    sender = "1234"; 
    body = "Android sms body"; 

    //Get my package name 
    final String myPackageName = getPackageName(); 

    //Check if my app is the default sms app 
    if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) { 

     //Get default sms app 
     String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context); 

     //Change the default sms app to my app 
     Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); 
     intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName()); 
     startActivity(intent); 

     //Write the sms 
     WriteSms(body, sender); 

     //Change my sms app to the last default sms app 
     Intent intent2 = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); 
     intent2.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); 
     startActivity(intent2); 
    } 
    else{ 

     //Write the sms 
     WriteSms(body, sender); 
    } 
} 

//Write the sms 
private void WriteSms(String message, String phoneNumber) { 

    //Put content values 
    ContentValues values = new ContentValues(); 
    values.put(Telephony.Sms.ADDRESS, phoneNumber); 
    values.put(Telephony.Sms.DATE, System.currentTimeMillis()); 
    values.put(Telephony.Sms.BODY, message); 

    //Insert the message 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     context.getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI, values); 
    } 
    else { 
     context.getContentResolver().insert(Uri.parse("content://sms/sent"), values); 
    } 
} 

好了,這是我想做的事,但我自己的應用程序,並與該應用Fake Text Message是下載到播放商店。

讓假消息,我應該看到我的默認短信應用是什麼:

+1

您的應用程序不能被設置爲默認的短信應用,除非在最低限度,其清單包含示例中顯示的所有內容:[讓您的短信應用程序準備好迎接KitKat](http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html ?M = 1)。另外,當'ACTION_CHANGE_DEFAULT'意圖被激發時,彈出一個確認對話框,所以我認爲你的方法不會像你期待的那樣工作。 – 2015-02-18 04:15:41

+1

作爲一個方面說明,如果您只是在運行4.4.4的自己的設備上玩耍,您可能會檢查[我的答案](http://stackoverflow.com/a/27709655/2850651)可能的解決方法。 – 2015-02-18 04:18:19

+0

我閱讀「讓您的短信應用程序準備好迎接KitKat」一文中顯示的示例。所有額外的東西,我不添加到我的清單,似乎是用來管理短信,彩信(recived,發送)與網絡(不是我想要在我的應用程序中做)。在我的應用程序,我只是想插入一個短信,如應用程序[假短信](https://play.google.com/store/apps/details?id=com.neurondigital.FakeTextMessage&hl=fr)。順便說一下,我想用我的應用程序比我的手機更多。 – Albentrix 2015-02-20 21:16:36

回答

9

與邁克M.我終於完成了我的程序的幫助。所以,這是你必須添加到您的應用程序代碼,以便能夠發送短信,而無需使用網:

清單:

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <!-- BroadcastReceiver that listens for incoming SMS messages --> 
    <receiver android:name=".SmsReceiver" 
     android:permission="android.permission.BROADCAST_SMS"> 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_DELIVER" /> 
     </intent-filter> 
    </receiver> 

    <!-- BroadcastReceiver that listens for incoming MMS messages --> 
    <receiver android:name=".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> 

    <!-- My activity --> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <!-- Activity that allows the user to send new SMS/MMS messages --> 
    <activity android:name=".ComposeSmsActivity" > 
     <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" /> 
      <data android:scheme="sms" /> 
      <data android:scheme="smsto" /> 
      <data android:scheme="mms" /> 
      <data android:scheme="mmsto" /> 
     </intent-filter> 
    </activity> 

    <!-- Service that delivers messages from the phone "quick response" --> 
    <service android:name=".HeadlessSmsSendService" 
     android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="sms" /> 
      <data android:scheme="smsto" /> 
      <data android:scheme="mms" /> 
      <data android:scheme="mmsto" /> 
     </intent-filter> 
    </service> 

</application> 

主要活動:

Context context; 
Button button; 
String sender,body,defaultSmsApp; 

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

    //Get current context 
    context = this; 

    //Set composant 
    button = (Button) findViewById(R.id.button); 

    //Get default sms app 
    defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context); 

    //Set the number and the body for the sms 
    sender = "0042"; 
    body = "Android fake message"; 

    //Button to write to the default sms app 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      //Get the package name and check if my app is not the default sms app 
      final String myPackageName = getPackageName(); 
      if (!Telephony.Sms.getDefaultSmsPackage(context).equals(myPackageName)) { 

       //Change the default sms app to my app 
       Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); 
       intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName()); 
       startActivityForResult(intent, 1); 
      } 
     } 
    }); 
} 

//Write to the default sms app 
private void WriteSms(String message, String phoneNumber) { 

    //Put content values 
    ContentValues values = new ContentValues(); 
    values.put(Telephony.Sms.ADDRESS, phoneNumber); 
    values.put(Telephony.Sms.DATE, System.currentTimeMillis()); 
    values.put(Telephony.Sms.BODY, message); 

    //Insert the message 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     context.getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI, values); 
    } 
    else { 
     context.getContentResolver().insert(Uri.parse("content://sms/sent"), values); 
    } 

    //Change my sms app to the last default sms 
    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); 
    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); 
    context.startActivity(intent); 
} 

//Get result from default sms dialog pops up 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == 1) { 
     // Make sure the request was successful 
     if (resultCode == RESULT_OK) { 

      final String myPackageName = getPackageName(); 
      if (Telephony.Sms.getDefaultSmsPackage(context).equals(myPackageName)) { 

       //Write to the default sms app 
       WriteSms(body, sender); 
      } 
     } 
    } 
} 

作爲的結果在清單中添加東西時,必須添加4個類:SmsReceiver,MmsReceiver,ComposeSmsActivity和HeadlessSmsSendService。您可以讓它們清空,如下所示。

SmsReceiver:

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

    } 
} 

MmsReceiver:

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

    } 
} 

ComposeSmsActivity:

public class ComposeSmsActivity extends ActionBarActivity { 

} 

HeadlessSmsSendService:

public class HeadlessSmsSendService extends IntentService { 
    public HeadlessSmsSendService() { 
     super(HeadlessSmsSendService.class.getName()); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
} 

如果您需要更多的幫助來理解這個程序看看有:

Youtube - DevBytes: Android 4.4 SMS APIs

Android developers - Getting Your SMS Apps Ready for KitKat

Possiblemobile - KitKat SMS and MMS supports

+0

頑皮調皮! – 130nk3r5 2015-07-24 13:03:22

+0

剛剛開始Android開發 - 我發現這個工作(短信在我們點擊按鈕後在收件箱中找到)。 但它沒有關於新短信的通知 - 每次我們點擊按鈕,我們都必須「確認」我們將使用這個應用程序作爲預設短信應用程序。 因此 - 當我們想要「體驗正常的SMS生命週期」(包含通知並且沒有持續的對話確認等)時,可能不適合用例。 – nettutvikler 2016-03-21 08:44:08

+0

@Albentrix不管我們設置[Read](https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns.html#READ)和[Seen](https://developer.android)的值。 com/reference/android/provider/Telephony.TextBasedSmsColumns.html#SEEN),因爲按照說它看到的文檔是用來設置新消息通知的。 – 2016-07-28 16:14:20