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 7939155
In Process

The Archive Base Latest Questions

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

I am trying to make a service and a notification for that. What I

  • 0

I am trying to make a service and a notification for that. What I have is two buttons. clicking on start button starts should start service and stop should stop. But right now they are not working. I have a main class, service class and notification class. here are my code and logcat output.

My service class is here

package com.zafar.batterynotify;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class BatteryService extends Service {
Notify notification = new Notify();

@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);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

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

}

Notification class

package com.zafar.batterynotify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Notify {
private static final int NOTIFICATION_ID = 1;

public void initNotification(Context actContext) {
    String ns = actContext.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) actContext.getSystemService(ns);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Service Started";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    //Context context = actContext.getApplicationContext();
    Context context = MyApplication.getContext();
    CharSequence contentTitle = "Ongoing service";
    CharSequence contentText = "This is service is ongoing";
    Intent notificationIntent = new Intent(context, BatteryNotify.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

public void cancelNotification(Context actContext) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) actContext.getSystemService(ns);
    mNotificationManager.cancel(NOTIFICATION_ID);
}
}

My logcat output

05-12 12:35:35.480: D/AndroidRuntime(27598): Shutting down VM
05-12 12:35:35.480: W/dalvikvm(27598): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-12 12:35:35.490: E/AndroidRuntime(27598): FATAL EXCEPTION: main
05-12 12:35:35.490: E/AndroidRuntime(27598): java.lang.RuntimeException: Unable to start service com.zafar.batterynotify.BatteryService@40526a48 with Intent { cmp=com.zafar.batterynotify/.BatteryService }: java.lang.NullPointerException
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.app.ActivityThread.access$2800(ActivityThread.java:117)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.os.Looper.loop(Looper.java:130)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at java.lang.reflect.Method.invoke(Method.java:507)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at dalvik.system.NativeStart.main(Native Method)
05-12 12:35:35.490: E/AndroidRuntime(27598): Caused by: java.lang.NullPointerException
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.content.ComponentName.<init>(ComponentName.java:75)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.content.Intent.<init>(Intent.java:2859)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at com.zafar.batterynotify.Notify.initNotification(Notify.java:24)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at com.zafar.batterynotify.BatteryService.onStartCommand(BatteryService.java:20)
05-12 12:35:35.490: E/AndroidRuntime(27598):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
05-12 12:35:35.490: E/AndroidRuntime(27598):    ... 10 more
05-12 12:35:56.465: D/AndroidRuntime(27706): Shutting down VM
05-12 12:35:56.465: W/dalvikvm(27706): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-12 12:35:56.470: E/AndroidRuntime(27706): FATAL EXCEPTION: main
05-12 12:35:56.470: E/AndroidRuntime(27706): java.lang.RuntimeException: Unable to start service com.zafar.batterynotify.BatteryService@4051f2f8 with Intent { cmp=com.zafar.batterynotify/.BatteryService }: java.lang.NullPointerException
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.app.ActivityThread.access$2800(ActivityThread.java:117)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.os.Looper.loop(Looper.java:130)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at java.lang.reflect.Method.invoke(Method.java:507)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at dalvik.system.NativeStart.main(Native Method)
05-12 12:35:56.470: E/AndroidRuntime(27706): Caused by: java.lang.NullPointerException
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.content.ComponentName.<init>(ComponentName.java:75)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.content.Intent.<init>(Intent.java:2859)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at com.zafar.batterynotify.Notify.initNotification(Notify.java:24)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at com.zafar.batterynotify.BatteryService.onStartCommand(BatteryService.java:20)
05-12 12:35:56.470: E/AndroidRuntime(27706):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
05-12 12:35:56.470: E/AndroidRuntime(27706):    ... 10 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-03T23:00:58+00:00Added an answer on June 3, 2026 at 11:00 pm

    In this line:

    Intent notificationIntent = new Intent(context, BatteryNotify.class);
    

    context is null.

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

Sidebar

Related Questions

So here's the problem : I'm trying to make a service that starts another
I am trying to make a service that spawns a desktop application, and then
I'm new to both Play! & Scala, but I'm trying to make a service
I am trying to make a simple windows service that maintains a queue of
i am trying to make a setup of windows service.but when i was building
I'm trying to make a simple room management service. The rooms have these properties:
I am trying to make a rest service that receives complex types from a
hi friends i was trying to make my service act dynamically... i have set
Trying to make a simple client that consumes a web service using RemObjects SDK,
I'm trying to make an app that uploads images to an S3 service. from

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.