【问题标题】:Automated SMS app crashes自动短信应用程序崩溃
【发布时间】:2015-06-30 03:12:23
【问题描述】:

我已经构建了一个测试应用程序,当点击按钮时,它会向预定义的电话号码发送带有预定义消息的自动 SMS。我使用了意图。

但是当我点击“按钮”时它崩溃了。有任何想法吗?我是否遗漏了什么或实施错误?

代码:

package com.ali.sms;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button button;

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

        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
                Sms();
            }
        });
    }

    void Sms(){

        Intent intent = new Intent( Intent.ACTION_SENDTO, Uri.parse("sms:0123456789"));
        intent.putExtra("sms_body", "Hello!");
        intent.setType("vnd.android-dir/mms-sms");
        startActivity(intent);
    }
}

Manifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ali.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="22" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
    </application>

</manifest>

在 Eclipse/LogCat 中出现此错误:

android.content.ActivityNotFoundException: 没有找到处理 Intent 的 Activity {act=android.intent.action.SENDTO typ=vnd.android-dir/mms-sms (has extras) }

手机上的错误信息是:

很遗憾,短信已停止。

附:我已经搜索了网络和 stackoverflow 以修复 No Activity found to handle Intent 但无济于事。

【问题讨论】:

    标签: android eclipse android-intent android-activity crash


    【解决方案1】:

    请检查此相关问题。 Send SMS via intent

    答案似乎是简单地删除意图类型。

     public void Sms() { 
         Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:0123456789")); 
         intent.putExtra("sms_body", "Hello!"); 
         startActivity(intent); 
      }
    

    为了自动发送,请考虑使用SmsManager.sendTextMessage 方法。

        SmsManager sm = SmsManager.getDefault();
        String destinationAddress = "012345679";
        String text = "Hello";
        sm.sendTextMessage(destinationAddress, null, text, null, null);
    

    【讨论】:

    • 好的,谢谢,它成功了。该应用程序不再崩溃,但仍然没有自动发送短信!它只是将我重定向到带有预先输入的消息和号码的默认短信应用程序:(
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多