Same ringtone plays twice
<p>I am making battery complete charged alarm app. Since I am new to android, so I have a problem in my code. When mobile is plugged in and when battery is charged, my code does play alarm(which is selected ringtone). But after some seconds that same alarm starts playing twice thus makes a weird sound. And when I unplug my mobile it then should stop playing alarm. But what it does is it stops one alarm but the second alarm keeps playing. Below is my code</p>

<pre><code>public class BatteryService extends Service {
Notify notification = new Notify();
BatteryAlarm alarm = new BatteryAlarm();
private MediaPlayer mMediaPlayer;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(this);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context c, Intent i) {

        int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

        SharedPreferences getAlarm = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String alarms = getAlarm.getString("ringtones", null); // /system/media/audio/ringtones/ANDROMEDA.ogg

        //Log.d("TAG", "Error!", alarms);
        Log.d("TAG", "alarms=" + alarms);
        //Log.e("TAG", alarms);

        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {

            if(level &lt; 100) {

                if(uri != null) {
                    playAlarm(c, uri);
                }
                //CharSequence t = (CharSequence) uri;
                //Toast.makeText(c, "Set", Toast.LENGTH_LONG).show();

            }

        } else if (plugged == 0) {
            if(uri != null) {
                stopAlarm();
            }
            Toast.makeText(c, "Unplugged", Toast.LENGTH_LONG).show();

        }
    }
};

private void playAlarm(Context c, Uri uri) {
    //Uri uri = Uri.parse(uri);
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(getBaseContext(), uri);
        final AudioManager audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        onDestroy();
    }
}

private void stopAlarm() {
    mMediaPlayer.stop();
    //onDestroy();
}

public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(this);
    //stopAlarm();
    unregisterReceiver(this.mBatInfoReceiver);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}
</code></pre>

<p>My logcat does not show any error except this line <code>05-13 10:44:24.270: E/MediaPlayer-JNI(29518): setDataSource: outside path in JNI is ?x@</code> which is appeared twice in my logcat and I have no idea what that means.</p>

<pre><code>05-13 10:44:19.165: D/dalvikvm(29518): GC_EXTERNAL_ALLOC freed 47K, 50% free 2725K/5379K, external 0K/0K, paused 28ms
05-13 10:44:19.270: D/dalvikvm(29518): GC_EXTERNAL_ALLOC freed 26K, 49% free 2788K/5379K, external 518K/518K, paused 33ms
05-13 10:44:19.345: D/CLIPBOARD(29518): Hide Clipboard dialog at Starting input: finished by someone else... !
05-13 10:44:24.265: D/TAG(29518): alarms=content://media/internal/audio/media/35
05-13 10:44:24.265: I/MediaPlayer(29518): uri is:content://media/internal/audio/media/35
05-13 10:44:24.265: I/MediaPlayer(29518): inside  getAudioFilePath: content://media/internal/audio/media/35
05-13 10:44:24.270: I/MediaPlayer(29518): The actual path is:/system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:24.270: I/MediaPlayer(29518): path is: /system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:24.270: I/MediaPlayer(29518): file path found for DRM file:path is: /system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:24.270: E/MediaPlayer-JNI(29518): setDataSource: outside path in JNI is ?x@
05-13 10:44:28.860: D/TAG(29518): alarms=content://media/internal/audio/media/35
05-13 10:44:28.860: I/MediaPlayer(29518): uri is:content://media/internal/audio/media/35
05-13 10:44:28.860: I/MediaPlayer(29518): inside  getAudioFilePath: content://media/internal/audio/media/35
05-13 10:44:28.875: I/MediaPlayer(29518): The actual path is:/system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:28.890: I/MediaPlayer(29518): path is: /system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:28.890: I/MediaPlayer(29518): file path found for DRM file:path is: /system/media/audio/ringtones/CASSIOPEIA.ogg
05-13 10:44:28.890: E/MediaPlayer-JNI(29518): setDataSource: outside path in JNI is ?x@
</code></pre>

<p><strong>Here is my main class may be the problem lies here</strong></p>

<pre><code>public class BatteryNotify extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.xml.prefs);
    addPreferencesFromResource(R.xml.prefs);

    SharedPreferences getCB = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox");

    /*
    boolean cb = getCB.getBoolean("checkbox", true);
    if(cb == true) {
        startService(new Intent(getBaseContext(), BatteryService.class));
        //Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_LONG).show();
    }
    */

    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if(newValue.toString().equals("true")) {
                //Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_LONG).show();
                startService(new Intent(getBaseContext(), BatteryService.class));
            } else {
                //Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
                stopService(new Intent(getBaseContext(), BatteryService.class));
            }
            return true;
        }
    });

}

}
</code></pre>
