我将在Android应用程序启动时启动服务。代码以下列方式编写。不幸的是,这段代码没有产生预期的效果。有人能告诉我这段代码有什么问题吗?非常感谢。
清单文件:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name=".BootTimeService">
<!--android:enabled="true"
android:exported="true" -->
<intent-filter>
<action android:name=".BootTimeService"/>
<!-- action android:name="android.intent.action.MAIN" / -->
<!-- category android:name="android.intent.category.LAUNCHER" / -->
</intent-filter>
</service>
<receiver
android:name=".RunBootTimeServiceReciver"
android:enabled="true">
<!--android:exported="true" -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!--category android:name="android.intent.category."/-->
</receiver>
</application>
</manifest>
BootTimeService类:
public class BootTimeService extends Service {
//region constants
private static final String TAG = BootTimeService.class.getSimpleName();
//public static final int SERVERPORT = 8080;
//endregion
//************************
public BootTimeService() {
}
//region override methods
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
//handleCommand(intent);
Log.i(TAG, "onStart");
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
Log.i(TAG, "onStartCommand");
Toast.makeText(getApplicationContext(), "onStartCommand", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
Toast.makeText(getApplicationContext(), "Service is created", Toast.LENGTH_LONG).show();
// try {
// server = HttpServer.create(new InetSocketAddress(SERVERPORT), 0);
// server.createContext("IS2", new IS2Handler());
// server.createContext("DSP", new DSPHandler());
// server.setExecutor(Executors.newFixedThreadPool(0x5) ); // creates a default executor
// server.start();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
//server.stop(0x1);
}
//endregion
//************************
}
RunBootTimeServiceReciver类:
public class RunBootTimeServiceReciver extends BroadcastReceiver {
// public RunBootTimeServiceReciver() {
// }
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("reciver", "Before start backgroud service");
Toast.makeText(context, "BackGroundService", Toast.LENGTH_LONG).show();
Intent startServiceIntent = new Intent(context, BootTimeService.class);
context.startService(startServiceIntent);
}
}
答案 0 :(得分:0)
您至少需要1项活动。此活动正在执行任何操作,但应在清单文件中指定。像这样:
修改强>
<强>的AndroidManifest.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boottimeapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.boottimeapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.boottimeapplication.BootTimeService" />
<receiver
android:name="com.example.boottimeapplication.RunBootTimeServiceReciver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
<强> MainActivity 强>
package com.example.boottimeapplication;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<强> RunBootTimeServiceReciver 强>
package com.example.boottimeapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class RunBootTimeServiceReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("reciver", "Before start backgroud service");
Toast.makeText(context, "BackGroundService", Toast.LENGTH_LONG).show();
Intent startServiceIntent = new Intent(context, BootTimeService.class);
context.startService(startServiceIntent);
}
}
<强> BootTimeService 强>
package com.example.boottimeapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class BootTimeService extends Service {
private static final String TAG = BootTimeService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
Toast.makeText(getApplicationContext(), "onStartCommand", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
Toast.makeText(getApplicationContext(), "Service is created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
}
}
答案 1 :(得分:0)
假设您在API级别&gt; = 13的设备上运行,您应该在CommonsWare之后阅读以下博文:http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
基本上,它表示只有在用户启动应用程序至少一次后才会收到任何广播。