我有一个需要在后台运行的间谍应用程序的客户端 - 接收收到的消息、发送的消息、gps 位置等。我可以在没有 Activity 用户界面的情况下启动服务吗?据我了解,我需要一个服务和接收器,我还需要打电话给接收器,比如说电池电量低,电池没问题 - 一些经常触发的意图。我怎样才能在模拟器上测试这个?
现在这就是我所拥有的
public class MyReceiver 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, MyService.class));
}
// TODO Auto-generated method stub
}
} }
对于接收器和
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...",Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...",
Toast.LENGTH_LONG).show();
}
}
为服务。我把它放在我的清单中
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.services"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="4" />
<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=".MyService"></service>
<receiver android:name=".MyReceiver"></receiver>
</application>
我走的好吗?