Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复android 6中的短信发送错误_Java_Android_Sms - Fatal编程技术网

Java 如何修复android 6中的短信发送错误

Java 如何修复android 6中的短信发送错误,java,android,sms,Java,Android,Sms,我已经做了一个应用程序,使用手机短信发送,阅读许可。它会在单击按钮时发送消息,并显示来自特定号码的传入消息 它在安卓5和更早版本的安卓上运行良好,但在安卓6上不起作用。当android 6用户按下按钮时,应用程序正在崩溃。我正在下面共享我的代码,请帮助我修复此错误 Main\u Activity.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="htt

我已经做了一个应用程序,使用手机短信发送,阅读许可。它会在单击按钮时发送消息,并显示来自特定号码的传入消息

它在安卓5和更早版本的安卓上运行良好,但在安卓6上不起作用。当android 6用户按下按钮时,应用程序正在崩溃。我正在下面共享我的代码,请帮助我修复此错误

Main\u Activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tti.traceanyvehiclewithdetails.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@drawable/ffff"
        android:alpha="0.4"
        android:scaleType="fitCenter"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/send_sms"
        android:minHeight="60dp"
        android:background="#f4843e"
        android:textColor="#000000"
        android:textSize="33dp"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView" />

    <EditText
        android:id="@+id/enterDetail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="1"
        android:gravity="top"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="74dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Enter Vehicle Number:"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:contextClickable="false"
        android:textSize="35dp"
        android:paddingTop="4dp"
        android:paddingLeft="3dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enterDetail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:longClickable="true"
        android:textSize="25dp"
        android:typeface="sans"
        android:contextClickable="true"
        android:clickable="true"
        android:textColor="#0066ff"
        android:layout_marginTop="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="2dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:textStyle="bold"
        android:textIsSelectable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Example: DL 15Y 2597"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="47dp"
        android:paddingLeft="6dp" />



    <!-- view for AdMob Banner Ad -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_below="@+id/textView1"
        android:layout_above="@+id/btnSendSMS"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignLeft="@+id/adView"
        android:layout_alignStart="@+id/adView"
        android:padding="60dp"
        android:layout_marginTop="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView3"
        android:layout_alignTop="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="#ee2467"
        android:textSize="25dp" />

</RelativeLayout>
import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends AppCompatActivity {
        InterstitialAd mInterstitialAd;

        Button btnSendSMS;
        EditText enteredNum;
        IntentFilter intentFilter;
        private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //---display the SMS received in the TextView---
                TextView loadingMsg = (TextView) findViewById(R.id.textView3);
                loadingMsg.setText("");
                spinner.setVisibility(View.GONE);
                TextView SMSes = (TextView) findViewById(R.id.textView1);
                SMSes.setText(intent.getExtras().getString("sms"));
            }
        };
        public ProgressBar spinner;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final TextView loadingMsg = (TextView) findViewById(R.id.textView3);
            intentFilter = new IntentFilter();
            intentFilter.addAction("SMS_RECEIVED_ACTION");
            btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
            spinner=(ProgressBar)findViewById(R.id.progressBar);
            spinner.setVisibility(View.GONE);

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    requestNewInterstitial();
                }
            });

            requestNewInterstitial();

            btnSendSMS.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    enteredNum = (EditText) findViewById(R.id.enterDetail);
                    assert enteredNum != null;
                    sendSMS("+917738299899", "VAHAN " + enteredNum.getText().toString().replaceAll(" ", "").toUpperCase());
                    Toast.makeText(getApplicationContext(), "Please wait while Loading Vehicle Details", Toast.LENGTH_LONG).show();
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                    spinner.setVisibility(View.VISIBLE);
                    loadingMsg.setText("Please wait while vehicle details are loading, it usually takes less than 30 seconds.");
                    TextView SMSes = (TextView) findViewById(R.id.textView1);
                    SMSes.setText("");
                }
            });


            // Load an ad into the AdMob banner view.
            AdView adView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder()
                    .setRequestAgent("android_studio:ad_template").build();
            adView.loadAd(adRequest);

        }

        private void requestNewInterstitial() {
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                    .build();

            mInterstitialAd.loadAd(adRequest);
        }

        @Override
        protected void onResume() {
    //---register the receiver---
            registerReceiver(intentReceiver, intentFilter);
            super.onResume();
        }
        @Override
        protected void onPause() {
    //---unregister the receiver---
            unregisterReceiver(intentReceiver);
            super.onPause();
        }
        private void sendSMS(String phoneNumber, String message)
        {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, null, null);
        }

    }
package tti.traceanyvehiclewithdetails;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
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 = "";
        int c=0;
        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(msgs[i].getOriginatingAddress().indexOf("VAAHAN")!= -1) {
                    c=1;
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                }
            }
            if(c==1) {
                //---send a broadcast intent to update the SMS received in the activity---
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("SMS_RECEIVED_ACTION");
                broadcastIntent.putExtra("sms", str);
                context.sendBroadcast(broadcastIntent);
            }
        }
    }
}
SMSReceiver.java:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tti.traceanyvehiclewithdetails.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@drawable/ffff"
        android:alpha="0.4"
        android:scaleType="fitCenter"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/send_sms"
        android:minHeight="60dp"
        android:background="#f4843e"
        android:textColor="#000000"
        android:textSize="33dp"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView" />

    <EditText
        android:id="@+id/enterDetail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="1"
        android:gravity="top"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="74dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Enter Vehicle Number:"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:contextClickable="false"
        android:textSize="35dp"
        android:paddingTop="4dp"
        android:paddingLeft="3dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enterDetail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:longClickable="true"
        android:textSize="25dp"
        android:typeface="sans"
        android:contextClickable="true"
        android:clickable="true"
        android:textColor="#0066ff"
        android:layout_marginTop="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="2dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:textStyle="bold"
        android:textIsSelectable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Example: DL 15Y 2597"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="47dp"
        android:paddingLeft="6dp" />



    <!-- view for AdMob Banner Ad -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_below="@+id/textView1"
        android:layout_above="@+id/btnSendSMS"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignLeft="@+id/adView"
        android:layout_alignStart="@+id/adView"
        android:padding="60dp"
        android:layout_marginTop="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView3"
        android:layout_alignTop="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="#ee2467"
        android:textSize="25dp" />

</RelativeLayout>
import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends AppCompatActivity {
        InterstitialAd mInterstitialAd;

        Button btnSendSMS;
        EditText enteredNum;
        IntentFilter intentFilter;
        private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //---display the SMS received in the TextView---
                TextView loadingMsg = (TextView) findViewById(R.id.textView3);
                loadingMsg.setText("");
                spinner.setVisibility(View.GONE);
                TextView SMSes = (TextView) findViewById(R.id.textView1);
                SMSes.setText(intent.getExtras().getString("sms"));
            }
        };
        public ProgressBar spinner;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final TextView loadingMsg = (TextView) findViewById(R.id.textView3);
            intentFilter = new IntentFilter();
            intentFilter.addAction("SMS_RECEIVED_ACTION");
            btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
            spinner=(ProgressBar)findViewById(R.id.progressBar);
            spinner.setVisibility(View.GONE);

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    requestNewInterstitial();
                }
            });

            requestNewInterstitial();

            btnSendSMS.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    enteredNum = (EditText) findViewById(R.id.enterDetail);
                    assert enteredNum != null;
                    sendSMS("+917738299899", "VAHAN " + enteredNum.getText().toString().replaceAll(" ", "").toUpperCase());
                    Toast.makeText(getApplicationContext(), "Please wait while Loading Vehicle Details", Toast.LENGTH_LONG).show();
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                    spinner.setVisibility(View.VISIBLE);
                    loadingMsg.setText("Please wait while vehicle details are loading, it usually takes less than 30 seconds.");
                    TextView SMSes = (TextView) findViewById(R.id.textView1);
                    SMSes.setText("");
                }
            });


            // Load an ad into the AdMob banner view.
            AdView adView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder()
                    .setRequestAgent("android_studio:ad_template").build();
            adView.loadAd(adRequest);

        }

        private void requestNewInterstitial() {
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                    .build();

            mInterstitialAd.loadAd(adRequest);
        }

        @Override
        protected void onResume() {
    //---register the receiver---
            registerReceiver(intentReceiver, intentFilter);
            super.onResume();
        }
        @Override
        protected void onPause() {
    //---unregister the receiver---
            unregisterReceiver(intentReceiver);
            super.onPause();
        }
        private void sendSMS(String phoneNumber, String message)
        {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, null, null);
        }

    }
package tti.traceanyvehiclewithdetails;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
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 = "";
        int c=0;
        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(msgs[i].getOriginatingAddress().indexOf("VAAHAN")!= -1) {
                    c=1;
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                }
            }
            if(c==1) {
                //---send a broadcast intent to update the SMS received in the activity---
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("SMS_RECEIVED_ACTION");
                broadcastIntent.putExtra("sms", str);
                context.sendBroadcast(broadcastIntent);
            }
        }
    }
}
package tti.traceAnyVehicle with details;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
公共类SMSReceiver扩展了BroadcastReceiver
{
@凌驾
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
int c=0;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i

对于Android 6.0及以上版本,您需要在运行时请求权限。 请参阅:

您也可以使用第三方库

看这里:


我必须获得什么许可?