Android后台进程(Service)
我希望任何人都知道这个问题,因为它让我发疯。
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 :
try :
在Service而不是Activity中实现
sendNotification()
(我自己的方法),这就是实现的方法。Implemented
sendNotification()
(my own method) in the Service instead of Activity, this is the way to do it.