自动短信应用程序崩溃
Automated SMS app crashes
我构建了一个测试应用程序,点击按钮后,它会向预定义的 phone 号码发送带有预定义消息的自动 SMS。我用过intents。
但是当我点击 'button' 时它崩溃了。有任何想法吗?我是不是遗漏了什么或实施有误?
代码:
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: No Activity found to handle Intent {act=android.intent.action.SENDTO typ=vnd.android-dir/mms-sms (has extras) }
在 phone 上的错误消息是:
Unfortunately, SMS has stopped.
P.S。我在网络和 Whosebug 上搜索了修复 没有 Activity 找到处理 Intent 但无济于事。
请检查这个相关问题。
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);
我构建了一个测试应用程序,点击按钮后,它会向预定义的 phone 号码发送带有预定义消息的自动 SMS。我用过intents。
但是当我点击 'button' 时它崩溃了。有任何想法吗?我是不是遗漏了什么或实施有误?
代码:
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: No Activity found to handle Intent {act=android.intent.action.SENDTO typ=vnd.android-dir/mms-sms (has extras) }
在 phone 上的错误消息是:
Unfortunately, SMS has stopped.
P.S。我在网络和 Whosebug 上搜索了修复 没有 Activity 找到处理 Intent 但无济于事。
请检查这个相关问题。 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);