无法完成在Android的引导侦听器后 [英] Can't complete After Boot listener in Android

查看:244
本文介绍了无法完成在Android的引导侦听器后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能不尽快启动服务的设备启动。
我的服务和接收器类如下。

 包android_programmers_guide.BroadcastReceiver1;
   进口的java.util.ArrayList;
   进口的java.util.List;
   进口java.util.Timer中;
   进口java.util.TimerTask中;
   进口android.app.ActivityManager;
   进口android.app.Service;
   进口android.content.Intent;
   进口android.content.pm.PackageManager;
   进口android.os.IBinder;
   进口android.util.Log;
   进口android.widget.Toast;
   公共类为MyService扩展服务{
 / **
  *延迟,直到日志任务的第一exeution。
  * /
 私人最终长mDelay = 0;
 / **
  *日志的任务期。
  * /
 私人最终长mPeriod = 500;
 / **
  *登录标签使用该服务。
  * /
 私人最终字符串LOGTAG =** ** BootDemoService;
 / **
  *定时调度服务。
  * /
 私人定时器mTimer; / **
  *定时任务的实现。
  * /
 私有类LogTask扩展的TimerTask {
  公共无效的run(){
   Log.i(LOGTAG计划);
  }
 }
 私人LogTask mLogTask; @覆盖
 公众的IBinder onBind(最终意向意图){
  返回null;
 } @覆盖
 公共无效的onCreate(){
  super.onCreate();
  Log.i(LOGTAG,创造);
  mTimer =新的Timer();
  mLogTask =新LogTask();
 } @覆盖
 公共无效调用onStart(最终意图的意图,最终诠释startId){
  super.onStart(意向,startId);
  Log.i(LOGTAG,启动);
  mTimer.schedule(mLogTask,mDelay,mPeriod);
 }
}

这是接收器类。

 包android_programmers_guide.BroadcastReceiver1;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
公共类MyStartupIntentReceiver扩展广播接收器{
 @覆盖
 公共无效的onReceive(最终上下文的背景下,最终的意图bootintent){
  意图mServiceIntent =新的Intent();
mServiceIntent.setAction(android_programmers_guide.BroadcastReceiver1.MyService);
  context.startService(mServiceIntent);
 }
}

这是清单文件。

 <?XML版本=1.0编码=UTF-8&GT?;
 <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=android_programmers_guide.BroadcastReceiver1
安卓版code =1机器人:=的versionName1.0>
<应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
    <服务机器人:为MyServiceNAME =>
        &所述;意图滤光器>
            <作用机器人:名字=android_programmers_guide.BroadcastReceiver1.MyService>
            < /作用>
        &所述; /意图滤光器>
    < /服务>
    <接收机器人:MyStartIntentReceiverNAME =>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.BOOT_COMPLETED>
            < /作用>
        &所述; /意图滤光器>
    < /接收器>
< /用途>
<采用-SDK安卓的minSdkVersion =4/>

请尽快回复,并在此先感谢。
干杯!

PS:我只从一个教程复制code和我使用Android的姜饼和Eclipse伽利略

编辑为你们说,我的服务是成功的。
但还有一个问题。
我无法启动的活动。我的code是如下:

 公共无效在onStart(最终意图的意图,最终诠释startId)
{
 super.onStart(意向,startId);
    意图int​​ent1 =新的Intent();
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(intent1);
}


解决方案

请添加该行的清单。

 <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED>< /使用许可权>

修改后:

 公共无效的onReceive(上下文的背景下,意图意图){
            // TODO自动生成方法存根
            如果(android.intent.action.BOOT_COMPLETED.equals(intent.getAction())){
                意图pushIntent =新意图(背景下,MyService.class);
                context.startService(pushIntent);            }   }

I can't start a service as soon as device boots. My service and receiver classes are as follows.

    package android_programmers_guide.BroadcastReceiver1;
   import java.util.ArrayList;
   import java.util.List;
   import java.util.Timer;
   import java.util.TimerTask;
   import android.app.ActivityManager;
   import android.app.Service;
   import android.content.Intent;
   import android.content.pm.PackageManager;
   import android.os.IBinder;
   import android.util.Log;
   import android.widget.Toast;
   public class MyService extends Service {
 /**
  * Delay until first exeution of the Log task.
  */
 private final long mDelay = 0;
 /**
  * Period of the Log task.
  */
 private final long mPeriod = 500;
 /**
  * Log tag for this service.
  */ 
 private final String LOGTAG = "**BootDemoService**";
 /**
  * Timer to schedule the service.
  */
 private Timer mTimer;

 /**
  * Implementation of the timer task.
  */
 private class LogTask extends TimerTask {
  public void run() {
   Log.i(LOGTAG, "scheduled");
  }
 }
 private LogTask mLogTask; 

 @Override
 public IBinder onBind(final Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  Log.i(LOGTAG, "created");
  mTimer = new Timer();
  mLogTask = new LogTask();
 }

 @Override
 public void onStart(final Intent intent, final int startId) {
  super.onStart(intent, startId);
  Log.i(LOGTAG, "started");
  mTimer.schedule(mLogTask, mDelay, mPeriod);
 }
}

This is receiver class.

package android_programmers_guide.BroadcastReceiver1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(final Context context, final Intent bootintent) {
  Intent mServiceIntent = new Intent();
mServiceIntent.setAction("android_programmers_guide.BroadcastReceiver1.MyService");
  context.startService(mServiceIntent);
 }
}

And this is manifest file.

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_programmers_guide.BroadcastReceiver1"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <service android:name=".MyService">
        <intent-filter>
            <action android:name="android_programmers_guide.BroadcastReceiver1.MyService">
            </action>
        </intent-filter>
    </service>
    <receiver android:name=".MyStartIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED">
            </action>
        </intent-filter>
    </receiver>
</application>
<uses-sdk android:minSdkVersion="4" />

Please reply ASAP and thanks in advance. Cheers!

PS: I only copy code from a tutorial and I am using Android Gingerbread with Eclipse Galileo.

After editing as you guys told, my service is succeed. But there's still a problem. I can't start an activity. My code is as follows:

public void onStart(final Intent intent, final int startId) 
{
 super.onStart(intent, startId);
    Intent intent1 = new Intent();
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(intent1);
}

解决方案

Please add that line in manifest.

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

After Edit:

public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {  
                Intent pushIntent = new Intent(context,MyService.class);  
                context.startService(pushIntent);           

            } 

   }

这篇关于无法完成在Android的引导侦听器后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆