Android后台进程(Service)

发布于 2024-11-03 08:40:42 字数 1723 浏览 4 评论 0原文

我希望任何人都知道这个问题,因为它让我发疯。

我有一个名为 IssueNotifier 的服务,它有一些方法:

public void onCreate()
{
    super.onCreate(); 
    startService();

    //initiate preferences to get values
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    choice = Integer.valueOf(prefs.getString("bgProcessPref", "15"));
    delay = choice * minute;
    if (VIEWISSUES_ACTIVITY != null)  
        Log.d(getClass().getSimpleName(), "ServiceTest started");
}

public void onDestroy()
{
    super.onDestroy();
    stopService();
    if (VIEWISSUES_ACTIVITY != null)  
        Log.d(getClass().getSimpleName(), "ServiceTest stopped");
}

private void startService() 
{ 
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() 
    {
        public void run() 
        {
            if(VIEWISSUES_ACTIVITY.getNewIssues() > 0)
                VIEWISSUES_ACTIVITY.sendNotification();
        }
    };
    timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run()
        { 
            handler.post(runnable);
            System.out.println(delay);
        } 

    }, delay, delay);
}

private void stopService()
{
    if(timer != null)
        timer.cancel();
}

VIEWISSUES_ACTIVITY 是另一个将启动此服务的 Activity,它有它的方法 sendNotification() ,其中如果有新消息到达,将通知用户:

    //just pass the reference to the service
    IssueNotifier.setMainActivity(this);

    //start the service
    startService(new Intent(this, IssueNotifier.class));

所有这些代码都工作正常,但我的问题是,当我尝试使用另一个活动启动服务时,我不希望 VIEWISSUES_ACTIVITY 启动此服务,而是另一个活动来启动此服务它不起作用,因为 sendNotification() 位于 VIEWISSUES_ACTIVITY 活动中(它必须在那里)。那么我怎样才能实现我的目标呢?

I hope anyone has any idea of this question because it has been driving me insane.

I have this service which I call IssueNotifier, and it has some methods in it:

public void onCreate()
{
    super.onCreate(); 
    startService();

    //initiate preferences to get values
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    choice = Integer.valueOf(prefs.getString("bgProcessPref", "15"));
    delay = choice * minute;
    if (VIEWISSUES_ACTIVITY != null)  
        Log.d(getClass().getSimpleName(), "ServiceTest started");
}

public void onDestroy()
{
    super.onDestroy();
    stopService();
    if (VIEWISSUES_ACTIVITY != null)  
        Log.d(getClass().getSimpleName(), "ServiceTest stopped");
}

private void startService() 
{ 
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() 
    {
        public void run() 
        {
            if(VIEWISSUES_ACTIVITY.getNewIssues() > 0)
                VIEWISSUES_ACTIVITY.sendNotification();
        }
    };
    timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run()
        { 
            handler.post(runnable);
            System.out.println(delay);
        } 

    }, delay, delay);
}

private void stopService()
{
    if(timer != null)
        timer.cancel();
}

VIEWISSUES_ACTIVITY is another Activity which will start this service, and it has its method sendNotification() which will notify the user if new messages has arrived:

    //just pass the reference to the service
    IssueNotifier.setMainActivity(this);

    //start the service
    startService(new Intent(this, IssueNotifier.class));

All this code works fine, but my problem is that I don't want VIEWISSUES_ACTIVITY to start this Service, but rather another Activity to do it, when I try to start the Service with another activity it doesn't work because sendNotification() is in the VIEWISSUES_ACTIVITY activity (it must be there). So how I can achieve my goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞陪衬 2024-11-10 08:40:42

尝试 :

Context.startService()

try :

Context.startService()

℉服软 2024-11-10 08:40:42

在Service而不是Activity中实现sendNotification()(我自己的方法),这就是实现的方法。

Implemented sendNotification() (my own method) in the Service instead of Activity, this is the way to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文