2011-10-07 212 views
74

从我在Stack Exchange和其他地方看到的所有内容中,当Android OS启动时,我已经正确设置了一切以启动IntentService。不幸的是,它不是在启动时启动,我没有得到任何错误。也许专家可以帮助...Android - 在启动时启动服务

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.phx.batterylogger" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:installLocation="internalOnly"> 

<uses-sdk android:minSdkVersion="8" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.BATTERY_STATS" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <service android:name=".BatteryLogger"/> 
    <receiver android:name=".StartupIntentReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
</application> 

</manifest> 

广播接收器的启动:

package com.phx.batterylogger; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class StartupIntentReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent serviceIntent = new Intent(context, BatteryLogger.class); 
     context.startService(serviceIntent); 
    } 
} 

更新:我试过几乎所有的下面的建议,和我说记录如Log.v("BatteryLogger", "Got to onReceive, about to start service");到StartupIntentReceiver的onReceive处理程序,并且没有任何记录。所以它甚至没有将它传送给BroadcastReceiver。

我想我正在部署APK并正确测试,只需在Eclipse中运行调试,并且控制台说它已成功将其安装到位于\ BatteryLogger \ bin \ BatteryLogger.apk的我的Xoom平板电脑上。然后进行测试,我重新启动平板电脑,然后查看DDMS中的日志,并在操作系统设置中检查运行服务。这一切听起来是正确的,还是我错过了什么?再次,任何帮助非常感谢。

+1

你得到什么问题,你没有得到任何UI ..? –

+0

该服务从未开始,这就是问题所在。 – Gady

+0

你怎么知道你的服务没有开始,你打印日志或类似的东西..? –

回答

230

那么这里是一个自动启动应用程序

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="pack.saltriver" android:versionCode="1" android:versionName="1.0" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     <receiver android:name=".autostart"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

     <activity android:name=".hello"></activity> 
     <service android:enabled="true" android:name=".service" /> 
    </application> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
</manifest> 

autostart.java

public class autostart extends BroadcastReceiver 
{ 
    public void onReceive(Context context, Intent arg1) 
    { 
     Intent intent = new Intent(context,service.class); 
     context.startService(intent); 
     Log.i("Autostart", "started"); 
    } 
} 

service.java

的一个完整的例子
public class service extends Service 
{ 
    private static final String TAG = "MyService"; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    public void onDestroy() { 
     Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onDestroy"); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) 
    { 
     Intent intents = new Intent(getBaseContext(),hello.class); 
     intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intents); 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onStart"); 
    } 
} 

hello.java - 每执行一次应用程序后启动设备,就会弹出。

public class hello extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show(); 
    } 
} 
+6

+1的完整例子。也许我应该改变我的IntentService与onHandleIntent到一个普通的旧服务 – Gady

+4

这最终解决了它。问题在于我的服务是IntentService而不是普通的旧服务和服务代码中的错误的组合。这个答案的完整性和登录Android帮助我解决了我的问题。 – Gady

+2

+1为详细的例子。如果没有活动,这也会起作用吗? – balas

1

看起来非常相似,mine,但我用的是全包名的接收器:

<receiver android:name=".StartupIntentReceiver"> 

我:

<receiver android:name="com.your.package.AutoStart"> 
+0

没有修复它。不过谢谢。 – Gady

+0

是的,虽然有点排序你的问题,所以更好地发布了一个你好世界的例子。 –

1

我已经没有完整的包成功,你知道呼叫链正在中断?如果您使用Log()进行调试,那么它在什么时候不再起作用?

我认为它可能在您的IntentService中,这一切都看起来不错。

+0

我添加了'Log.v(“BatteryLogger”,“有onReceive,即将开始服务”);'onReceive处理程序,它从不出现在日志中。因此,听众失败(?) – Gady

+0

当手机启动时,广播接收机是什么启动的?谢谢! –

+0

男孩。几年后我还没有碰到过Android。对不起@Ruchir – Phix

3

以下应该工作。我已验证。可能是你的问题在别的地方。

public class MyReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("TAG", "MyReceiver"); 
     Intent serviceIntent = new Intent(context, Test1Service.class); 
     context.startService(serviceIntent); 
    } 
} 




public class Test1Service extends Service { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d("TAG", "Service created."); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.d("TAG", "Service started."); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Log.d("TAG", "Service started."); 
    } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
} 




<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test" 
     android:versionCode="1" 
     android:versionName="1.0" 
     android:installLocation="internalOnly"> 
    <uses-sdk android:minSdkVersion="8" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.BATTERY_STATS" 
    /> 
<!--  <activity android:name=".MyActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER"></category> 
      </intent-filter> 
     </activity> --> 
     <service android:name=".Test1Service" 
        android:label="@string/app_name" 
        > 
     </service> 
     <receiver android:name=".MyReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 
+0

谢谢你,但它仍然无法正常工作。日志甚至不显示它使它进入StartupIntentReceiver。任何其他想法? – Gady

+0

请参阅我的问题的更新。 – Gady

+1

你的代码在模拟器中工作吗?你能够在模拟器中接收意图吗? – Vivek

2

服务可能会被越来越关闭它完成,由于设备会启动后睡觉前。您需要先获得唤醒锁。幸运的是,Support library gives us a class要做到这一点:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // This is the Intent to deliver to our service. 
     Intent service = new Intent(context, SimpleWakefulService.class); 

     // Start the service, keeping the device awake while it is launching. 
     Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime()); 
     startWakefulService(context, service); 
    } 
} 

然后,在你的服务,确保释放唤醒锁:

@Override 
    protected void onHandleIntent(Intent intent) { 
     // At this point SimpleWakefulReceiver is still holding a wake lock 
     // for us. We can do whatever we need to here and then tell it that 
     // it can release the wakelock. 

... 
     Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime()); 
     SimpleWakefulReceiver.completeWakefulIntent(intent); 
    } 

不要忘记添加WAKE_LOCK权限:

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

我有同样的问题。你介意帮助我吗?谢谢! http://stackoverflow.com/questions/35373525/starting-my-service –