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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:18:49+00:00 2026-05-26T07:18:49+00:00

I’m trying to create a service which will start by the user request in

  • 0

I’m trying to create a service which will start by the user request in the application.
After the user will choose an update interval, the service will run in the operation system background, and will send a non-relevant message.
I’ve tried to write the service according to the example for Service class API.
For some reason, I figured in debug (when running doBindService() method) that mUpdateBoundService is getting null.
My second question is whether I can use “Toast” inform message outside an application ? (As kind of a desktop notification).
Can anyone help ? Here is my short code:

UpdateService.java

package android.update;

import java.util.Timer;

import java.util.TimerTask;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.widget.Toast;

public class UpdateService extends Service {
    private NotificationManager mNM;

    private final IBinder mBinder = new UpdateBinder();
    private int updateInterval;    

    public class UpdateBinder extends Binder {
        UpdateService getService() {
            return UpdateService.this;
        }
    }

    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Timer timer = new Timer();
        timer.schedule(new UpdateTimeTask(), 100, updateInterval);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {        
        return START_STICKY;
    }

    class UpdateTimeTask extends TimerTask {
           public void run() {
               showNotification();
           }
        }

    public void showNotification() {        
        Toast.makeText(this, "Hi", 10);
    }

    @Override
    public IBinder onBind(Intent intent) {
        updateInterval = intent.getExtras().getInt(getString(R.string.keyUpdateInterval));
        return mBinder;
    }
}

UpdateActivity.java

package android.update;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

public class UpdateActivity extends Activity {

    private UpdateService mUpdateBoundService;
    private boolean mIsBound = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClickStartUpdateService(View view) {
        switch (view.getId()) {
        case R.id.btnStartUpdateService:
            doBindService();
            //Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
            mUpdateBoundService.showNotification();
            break;
        }
    }    

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mUpdateBoundService = ((UpdateService.UpdateBinder)service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            mUpdateBoundService = null;
        }
    };

    private void doBindService() {
        Intent updateActivityIntent = new Intent(UpdateActivity.this, 
                UpdateService.class);       
        EditText txtUpdateInterval = (EditText) findViewById(R.id.txtUpdateInterval);
        int interval = Integer.parseInt(txtUpdateInterval.getText().toString());
        updateActivityIntent.putExtra(getString(R.string.keyUpdateInterval), interval);
        bindService(updateActivityIntent, mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {             
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
}
  • 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-05-26T07:18:50+00:00Added an answer on May 26, 2026 at 7:18 am

    Your toast is not showing because you are not telling it to. Try:

    public void showNotification() {        
        Toast.makeText(this, "Hi", 10).show();
    }
    

    For your service issue, I think that you do not properly understand how services & activities work together. A service can run independently of a service, or you can have a service whose lifecycle matches that of a given activity. From your code, it is not clear which of these models you are following. Your implementation will cause the service to wake periodically, but only while your activity is running. If the user switches to another activity, your service will no longer be woken.

    If you want a service to wake periodically independently of the activity, then you need to run your timer event in the service itself. Better still use an Alarm to wake your service: Register an Alarm with AlarmManager which will fire an Intent at a future point (or regular intervals, if you prefer), and extend your service from IntentService, override onHandleIntent() and add the necessary Intent Filter to your Service entry in the manifest.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters 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.