大家好,我在这段代码中遇到了一些问题
这是我的主要活动,我点击添加按钮显示我的SMs屏幕,但它总是崩溃
package com.example.project.ultimatesmsblocker;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void show(View view){
Intent intent = new Intent(MainActivity.this, SmsReceiver.class);
startActivity(intent); }}
这是我的短信接收器类
package com.example.project.ultimatesmsblocker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
/**
* Created by GhulamMustafa on 6/21/2016.
*/
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in--
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "SMS from ";
if (bundle != null)
{
//---retrieve the SMS message received--
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (i==0) {
//---get the sender address/phone number--
str += msgs[i].getOriginatingAddress();
str += ": ";
}
//---get the message body--
str += msgs[i].getMessageBody().toString();
}
//---display the new SMS message--
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---prevent this SMS message from being broadcasted--
abortBroadcast();
Log.d("SMSReceiver", str);
}
try {
}
catch(Exception e){
Toast.makeText(context,"No Message",Toast.LENGTH_LONG).show();
}}}
这是Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project.ultimatesmsblocker">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".BlockList">
</activity>
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="100" >
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
当我编译它并点击添加按钮时,它总是会尽快帮助...我将非常感谢你...