首页 > 代码库 > 【转】android 手机发送短信,震动,播放默认铃声

【转】android 手机发送短信,震动,播放默认铃声

发送短信

@SuppressWarnings("deprecation")
 public void sendMessage(String phoneNo, String messageContent)
 {
          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage(phoneNo,// 接收方的手机号码
                                                                   null,// 发送方的手机号码
                                                                   messageContent,// 信息内容
                                                                   null,// 发送是否成功的回执
                                                                   null// 接收是否成功的回执
                                                                  );
    }

   //播放默认铃声
    private MediaPlayer mediaPlayer = new MediaPlayer();
    public boolean ring()
    {
             Uri ringToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
             try
             {
                 mediaPlayer.setDataSource(this, ringToneUri);

                 final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                 if (audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) != 0) 
                 {
                       mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
                       mediaPlayer.setLooping(false);
                       mediaPlayer.prepare();
                       mediaPlayer.start();
                       return true;
                 }
             }
             catch(Exception e)
            { 
                  e.printStackTrace(); 
            }
             return false;
    }

    //停止播放铃声
    public void stopRing()
    {
             mediaPlayer.stop();
    }

    //震动
    public void vibrate(int ms)//输入震动毫秒数
   {
             Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);

             vibrator.vibrate(ms);     
    }

使用时要注意在AndroidManifest.xml中添加权限

 发送短信权限

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

震动权限
 <uses-permission android:name="android.permission.VIBRATE"/>

具体实现可以封装在Service中提供调用,如何实现请看android Service调用,传参。

【转】android 手机发送短信,震动,播放默认铃声