Android Reboot Simulation:服务无法正常工作

时间:2013-06-07 17:57:45

标签: android android-emulator android-service

我在这里点了这篇文章How to reboot emulator to test ACTION_BOOT_COMPLETED?

我实施了那里列出的所有步骤。但是我的服务在重新启动时仍然没有运行。

我正在尝试将LaucherActivty转换为Service。当我在模拟器上单击Laucher图标时,Laucher活动正常工作。但是,当我将其转换为服务并重新启动模拟器时,它不起作用。

这是我的代码:

BootReceiver

package my.app.client;

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

public class BootReceiver extends BroadcastReceiver {

    public final String TAG = BootReceiver.class.getSimpleName();
    //Intent Client, ClientAlt;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG,"BOOT Complete received by Client !");

        String action = intent.getAction();

        if(action.equals(Intent.ACTION_BOOT_COMPLETED)) { //android.intent.action.BOOT_COMPLETED
            Intent serviceIntent = new Intent(context, Client.class);
            serviceIntent.setAction(BootReceiver.class.getSimpleName());
            //context.startService(serviceIntent);              
            //Client.setAction(LauncherActivity.class.getName());
            serviceIntent.putExtra("IP", "127.0.0.1");
            serviceIntent.putExtra("PORT", 9999);           
            context.startService(serviceIntent); }
        }
    }

AndroidManifest

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.app.client"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />


    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- Pour récupérer l'IMEI et toutes ses informations -->
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <!-- Pour récupérer les appels sortant -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><!-- Pour savoir s'il l'on est connecté ou pas -->


    <uses-permission android:name="android.permission.INTERNET" /><!-- Pour utiliser les sockets -->


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!--  Pour plus tard avec SD card -->



    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><!-- To start just after Boot -->



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


        <receiver android:name="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

        <service android:name="my.app.client.Client" >
            <intent-filter>
                <action android:name=".Client" />
            </intent-filter>
        </service>

        <receiver android:name="my.app.client.AlarmListener">
        </receiver>

        <activity android:name="my.app.client.LauncherActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="my.app.alt.PhotoActivity" android:label="@string/app_name" >
        </activity>

    </application>

</manifest>

LauncherActivity

package my.app.client;

import my.app.client.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LauncherActivity extends Activity {
    /** Called when the activity is first created. */

    Intent Client, ClientAlt;
    //Button btnStart, btnStop;
    //EditText ipfield, portfield;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Client = new Intent(this, Client.class);
        Client.setAction(LauncherActivity.class.getName());

        //btnStart = (Button) findViewById(R.id.buttonstart);
        //btnStop = (Button) findViewById(R.id.buttonstop);
        //ipfield = (EditText) findViewById(R.id.ipfield);
        //portfield = (EditText) findViewById(R.id.portfield);


                Client.putExtra("IP", "127.0.0.1");
                Client.putExtra("PORT", 9999);
                startService(Client); }
}
               // btnStart.setEnabled(false);
               // btnStop.setEnabled(true);
                //finish();                

2 个答案:

答案 0 :(得分:0)

将您的接收器注册为xml,如下所示

<receiver android:name="my.app.client.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

答案 1 :(得分:0)

将清单文件中的接收方注册为“.BootReceiver”而不是“BootReceiver”