0

我在这里关注了这篇文章如何重新启动模拟器来测试 ACTION_BOOT_COMPLETED?

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

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

这是我的代码:

引导接收器

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); }
        }
    }

安卓清单

  <?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>

启动器活动

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();                
4

2 回答 2

0

像这样在xml中注册你的接收者

<receiver android:name="my.app.client.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
于 2013-06-07T18:02:22.297 回答
0

Register your receiver in the manifest file as ".BootReceiver" and not "BootReceiver"

于 2016-02-03T21:03:32.443 回答