Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:35:00+00:00 2026-06-03T20:35:00+00:00

I am trying to play a ringtone when battery is completely charged. To acheive

  • 0

I am trying to play a ringtone when battery is completely charged. To acheive this I made a service. When I execute my code for the first time it worked then for the second time it didnt and I keep getting these errors in my logcat.

05-13 10:03:13.925: D/dalvikvm(26079): GC_EXTERNAL_ALLOC freed 25K, 49% free   2788K/5379K, external 518K/518K, paused 29ms
05-13 10:03:13.985: D/CLIPBOARD(26079): Hide Clipboard dialog at Starting input: finished by someone else... !
05-13 10:03:16.210: D/AndroidRuntime(26079): Shutting down VM
05-13 10:03:16.210: W/dalvikvm(26079): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:03:16.215: E/AndroidRuntime(26079): FATAL EXCEPTION: main
05-13 10:03:16.215: E/AndroidRuntime(26079): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@4053f718
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Looper.loop(Looper.java:130)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079): Caused by: java.lang.NullPointerException: uriString
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.<init>(Uri.java:420)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.<init>(Uri.java:410)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri.parse(Uri.java:382)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:49)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:03:16.215: E/AndroidRuntime(26079):    ... 9 more

According to logcat error is somewhere on Uri uri = Uri.parse(alarms);.

Here is my code for the service class

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);
        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {

            if(level < 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);
    unregisterReceiver(this.mBatInfoReceiver);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}

Help me please. Where am I making mistake due to which I am keep getting these errors

Logcat after adding Log.d(“TAG”, alarms)

05-13 10:29:43.450: D/AndroidRuntime(28062): Shutting down VM
05-13 10:29:43.450: W/dalvikvm(28062): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:29:43.455: E/AndroidRuntime(28062): FATAL EXCEPTION: main
05-13 10:29:43.455: E/AndroidRuntime(28062): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@40547e78
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Looper.loop(Looper.java:130)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062): Caused by: java.lang.NullPointerException: println needs a message
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.println_native(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.d(Log.java:137)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:51)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:29:43.455: E/AndroidRuntime(28062):    ... 9 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:35:02+00:00Added an answer on June 3, 2026 at 8:35 pm

    I suspect this is the problem:

    String alarms = getAlarm.getString("ringtones", null);
    Uri uri = Uri.parse(alarms);
    

    It looks like alarms is still null – so presumably there’s no “ringtones” string in the shared preferences.

    First you should validate that that’s the case (log alarms before you try to parse it) and then work out why the expected preference wasn’t present.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to play around with this for a long time and
i am trying folling code into android tv=(TextView) findViewById(R.id.ringtone); if (!hasErrors) { setListAdapter(new ArrayAdapter<String>(FindFilesByType.this,
iam trying play animation with xcode,in specefic Time for example after 3 minutes play
I am trying to play apple test stream video (.m3u8) in Android. This is
I'm trying to play around with an Android Service but the more I play
I am trying to play a ringtone which is selected from a RingtonePreference. How
I am trying to play a movie fullscreen one time, then close the player
I'm trying to play a sound file from an iPhone program. Here's the code:
I am trying to play some audio files with the CLI example on this
I'm trying to play a local mpeg2 TS file with gstreamer with this: gst-launch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.