我正在测试简单的警报应用程序。在第一个活动中,我使用从 Ringtonemanger 挑选的铃声设置警报,为此我使用了 setRing()方法。然后通过意图我将它传递给广播接收器,但是当在特定时间调用警报时,然后在player.setAudioStreamType(AudioManager.STREAM_ALARM) ** 它抛出 **NULL 指针异常。有人知道吗?
private void setRing()
{
intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
this.chosenRingtone = uri.toString();
}
else
{
this.chosenRingtone = null;
}
}
public class GroupsCheckAlarmReceiver extends BroadcastReceiver {
MediaPlayer mMediaPlayer ;
MediaPlayer player;
Context context1;
Uri uri=null;
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "Alarm success", Toast.LENGTH_LONG).show();
String struri=intent.getStringExtra("uristr");
Log.v("value of ring",struri);
uri=Uri.parse(struri);
context1=context;
try {
callringtone();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void callringtone() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context1,uri);
final AudioManager audioManager = (AudioManager)context1.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
}
if(uri == null){
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(uri== null){
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
}
}