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

  • Home
  • SEARCH
  • 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 7858081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:15:54+00:00 2026-06-02T21:15:54+00:00

What i am trying to do is send a notification from within a service

  • 0

What i am trying to do is send a notification from within a service whenever the location is changed. When the user taps on the notification i want the notification to close and start an activity. I managed to send the notification but when the notification is tapped it doesn’t clear nor it starts the activity. I can’t see where i am going wrong with this!? Here is the code:

package com.oxinos.android.moc;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;


public class mocService extends Service implements OnClickListener{

    NotificationManager nm;
    static final int uniqueID1= 190910;
    PendingIntent pi;
    Context con;

    @Override
    public IBinder onBind(Intent intent) {

        return null;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        con = getApplicationContext();

        Intent intent = new Intent(this, mocActivity2.class);

        pi = PendingIntent.getService(this, 0, intent, 0);
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                String locStr = "New loc: "+location.getLatitude()+","+location.getLongitude();
                String title = "New MOC notification";

                Notification startupNotification = new Notification(R.drawable.ic_launcher, locStr, System.currentTimeMillis());
                startupNotification.setLatestEventInfo(con, title,locStr, pi);
                startupNotification.defaults = Notification.DEFAULT_ALL;
                nm.notify(uniqueID1, startupNotification);
                //nm.cancel(uniqueID1);

            }

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }
        };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100000, 50, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100000 , 50, locationListener);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);

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

    }

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        startActivity(new Intent(this, mocActivity2.class));
        nm.cancel(uniqueID1);
    }
}

Any ideas??? What am i doing wrong?

  • 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-02T21:15:56+00:00Added an answer on June 2, 2026 at 9:15 pm

    You are creating your PendingIntent using getService(). If you want to start an activity with the PendingIntent, you need to use getActivity().

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

Sidebar

Related Questions

I trying to use apns-csharp library to send push notification from .NET, I created
I'am trying to send push notification from my localhost to my iDevices all work
I'm trying to send emails from my Grails 2.0 app via Sendgrid on Heroku
I am trying to send a dictionary of information from one iphone to another
I am trying to send a sample email from my rails application. this is
I am trying to send notification emails(which is working fine) but have added the
I am trying to send a location update with the new location as a
I am trying to get the emails I send through Gmail from my Ruby
I am trying to send growl notifications from PHP. The receiving computer is OSX
I'm trying to send an email from a CakePHP shell just as you would

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.