有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我的Android应用程序中的java SMS按钮不仅适用于Nexus 5和Android 4.4.2(KitKat)

我的应用程序中有两个按钮。一个用于拨号,另一个用于使用sms发送短消息。 拨号按钮工作正常,但是sms按钮在Android 4.4.2的Nexus 5上不起作用

在其他手机上,我没有同样的问题

使用模拟器也没有问题。在Nexus上,我得到了

SMS faild, please try again later.

我已经在^{中设置了权限

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

感谢您,并对所有英语错误表示歉意:-)

这是我的Java代码:

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.telephony.PhoneStateListener;
import 安卓.telephony.TelephonyManager;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.Toast;

public class TeamBerndK extends Activity implements OnClickListener {

final Context context = this;
private Button phonebtn, smsbtn;

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

    phonebtn = (Button) findViewById(R.id.callberndk);
    smsbtn = (Button) findViewById(R.id.smsberndk);

    PhoneCallListener phoneCallListener = new PhoneCallListener();
    TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    telManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);

     // add button listener

    phonebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

            Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
            phoneCallIntent.setData(Uri.parse("tel:0176********"));
            startActivity(phoneCallIntent);

        }
    });

    smsbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        sendSMS();
     }

        private void sendSMS() {

            Log.i("Send SMS", "");

              Intent smsIntent = new Intent(Intent.ACTION_VIEW);
              smsIntent.setData(Uri.parse("smsto:"));
              smsIntent.setType("vnd.安卓-dir/mms-sms");

              smsIntent.putExtra("address"  , new String ("0176********"));
              try {
                 startActivity(smsIntent);
                 finish();
                 Log.i("Finished sending SMS...", "");
              } catch (安卓.content.ActivityNotFoundException ex) {
                 Toast.makeText(TeamBerndK.this, 
                 "SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
              }

        }
  });

}   

@Override
public void onClick(View v) {
    finish();
}

private class PhoneCallListener extends PhoneStateListener {

    String TAG = "LOGGING PHONE CALL";
    private boolean phoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {

            // phone ringing
            Log.i(TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {

            // active
            Log.i(TAG, "OFFHOOK");

            phoneCalling = true;
        }

        // When the call ends launch the main activity again
        if (TelephonyManager.CALL_STATE_IDLE == state) {
            Log.i(TAG, "IDLE");
            if (phoneCalling) {
                Log.i(TAG, "restart app");

                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());

                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                 phoneCalling = false;
            }
        }
    }
}
}

共 (0) 个答案