2016-03-08 52 views
0

我做了消息发送应用程序它的工作完美的所有版本,直到kitkat 4.4.4但不工作在棒棒糖和棉花糖我不明白为什么?消息发送代码工作到Android 4.4.4,但不工作在lolipop和marshmallo

公共无效sendsms(){

try 
    { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNo, null,smsmessage, null, null); 
     printmsg(1); 
    } 
    catch (Exception e) 
    { 
     printmsg(0); 
     e.printStackTrace(); 
    } 
} 
public void printmsg(int a) 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Information:"); 

    if(a==1) 
     builder.setMessage("SMS Send Wait For Response!!!"); 
    else 
     builder.setMessage("Sending Failed, Please Try Again Later!!!"); 

    builder.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int which) 
     { 
      //Toast.makeText(getApplicationContext(), "changing activity",Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(ResWindow.this, Home.class); 
      startActivity(intent); 
      ResWindow.this.finish(); 

     } 
    }); 
    builder.show(); 
+0

“不工作”是什么意思?你的症状是什么?例如,你崩溃了吗?如果是这样,你的堆栈跟踪是什么? – CommonsWare

+0

添加这个 - Log.i(“Message not sent”,“”);在catch块内。 – SanVed

+0

去在其他条件“发送失败再试一次”和消息不会和kitkat工作好消息发送工作和播种适当的消息“消息发送成功等待响应” –

回答

1

从API 23,即Android的棉花糖,应用程序将要问的权限,而不是运行事先声明他们。这里的逻辑是,看看设备是否低于23,如果是,那么SMS将毫无问题地发送,如果23和更高,你将不得不提示用户允许应用程序发送消息。

下面是一个示例代码和解释可能会帮助你。

private static final int PERMISSION_REQUEST = 100; 

//This is the onClick listener of 'Send SMS' button 
public void send(View view) { 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     //This if checks if the device is API 23 or above 
     if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { 
      //This checks if the permission is already granted. 
      if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) { 
       //This displays a message to the user as to why do we 
       //need the permission. If the user accepts, we display the permission granting dialog. 
       Snackbar.make(findViewById(R.id.rl), "You need to grant SEND SMS permission to send sms", 
         Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST); 
        } 
       }).show(); 
      } else { 
       //This displays the permission granting dialog directly. 
       requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST); 
      } 
     } else { 
      sendSMS(); // Runs if the permission is already granted. 
     } 
    } else { 
     sendSMS(); // Runs if the device's API is below 23. 
    } 
} 

private void sendSMS() { 
    //Code for sending sms. 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     Snackbar.make(findViewById(R.id.rl), "Permission Granted", 
       Snackbar.LENGTH_LONG).show(); 
     sendSMS(); 
     //SMS sent 
    } else { 
     Snackbar.make(findViewById(R.id.rl), "Permission denied", 
       Snackbar.LENGTH_LONG).show(); 
     //SMS not sent. 
    } 
} 

的代码here拍摄。那里可以找到更详细的解释。

相关问题