3 回答
TA贡献1817条经验 获得超14个赞
不幸的是,在您的应用程序安装之后,目前没有可靠的方法来接收广播事件,ACTION_PACKAGE_ADDED Intent不会广播到新安装的程序包。
为了接收ACTION_BOOT_COMPLETED事件,您将必须具有广播接收器类以及您的服务。我还建议添加该广播接收器要捕获的ACTION_USER_PRESENT意图,这需要Android 1.5(minSDK = 3),只要用户解锁手机,这都会调用广播接收器。要使服务正常运行而又不容易被自动关闭,您可以做的最后一件事是在服务的onCreate上调用Service.setForeground()来告诉Android您不应停止该服务,这主要是添加了用于必须保持运行但可以被任何服务使用的mp3播放器类型的服务。
确保为清单中的boot_complete和user_present事件添加适当的权限。
这是一个简单的类,可以用作事件的广播接收器。
package com.snctln.util.WeatherStatus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class WeatherStatusServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() != null)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
context.startService(new Intent(context, WeatherStatusService.class));
}
}
}
};
祝好运。
- 3 回答
- 0 关注
- 305 浏览
添加回答
举报