小编sky*_*sky的帖子

从最近的应用程序中删除应用程序后,AlarmManager停止

我是android的新手,在这里我的目标是使用警报管理器每2分钟运行一个代码段,该代码段将轮询服务器(使用网站的api),并基于返回的JSON生成通知。上网查找后,我认为最好的选择之一就是使用Intent服务和android。

服务和收据清单

<service
    android:name=".NotifyService"
    android:enabled="true"
    android:exported="false" >
</service>
<receiver
    android:name=".TheReceiver"
    android:enabled="true"
    android:exported="true" >
</receiver>
<receiver
    android:name=".OnOffReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

在Flash屏幕活动的一部分中,我将其称为意图服务,该服务负责轮询通知:

Intent msgIntent = new Intent(this, NotifyService.class);
    startService(msgIntent);
Run Code Online (Sandbox Code Playgroud)

接收器在设备启动时启动警报:

public class OnOffReceiver extends BroadcastReceiver
{
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    public OnOffReceiver(){}
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, NotifyService.class);
        service.setAction(NotifyService.CREATE);
        context.startService(service);
    }
} 
Run Code Online (Sandbox Code Playgroud)

IntentService类别

public class NotifyService extends IntentService
{
    public NotifyService()
    {
        super("NotifyService");
    }
    public static final …
Run Code Online (Sandbox Code Playgroud)

android alarmmanager android-service intentservice android-background

1
推荐指数
1
解决办法
2131
查看次数