2016-03-08 36 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拍攝。那裏可以找到更詳細的解釋。

相關問題