简体   繁体   English

我无法在Android中使用boot_completed服务

[英]I cannot use boot_completed service in android

I want to use services in android.I worte code as a simple 我想在android中使用服务。我把代码写得很简单

That's my mainactivity class; 那是我的主要活动课;

public class MainActivity extends Activity 

{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public class MReceiver extends BroadcastReceiver 
{        

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(context, MService.class);
         context.startService(myIntent);
    } 
}

public class MService extends Service
{

    @Override
    public IBinder onBind(Intent intent) 
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        Log.i("onCreate", "Service onCreate");
    }

}

}

and that's my xml file; 那是我的xml文件;

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

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

        <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" >
            <activity
                android:name="com.example.service.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:enabled="true" android:name=".MService" />

             <receiver android:name=".MReceiver" android:enabled="true" android:exported="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>

        </application>

    </manifest>

But when I open my tablet after close that program is crashing and there 但是当我关闭后打开平板电脑时,该程序崩溃了

error is ; 错误是;

"error opening trace file" "unable to instantiate receiver com.example.service.MReceiver" “错误打开跟踪文件”“无法实例化接收器com.example.service.MReceiver”

What must I do ? 我必须做什么 ? Please help me. 请帮我。

You can declare your Receiver class as static class in Activity... 您可以在Activity中将Receiver类声明为静态类。

public static class MReceiver extends BroadcastReceiver {        

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MService.class);
     context.startService(myIntent);
} 
}

or write Receiver class in separate java file... 或在单独的Java文件中编写Receiver类...

if you write it in MainActivity, write it in maifest like 如果您在MainActivity中编写它,请以最类似的形式编写

 <receiver name=".MainActivity$MReceiver" ....>

or if it is a separate file, declare like 或者,如果它是一个单独的文件,则声明为

  <receiver name=".MReceiver" ...>

(here make sure that your MReceiver class package name and application package name are same) (在这里,请确保您的MReceiver类软件包名称和应用程序软件包名称相同)

In your manifest you have declared your <receiver> with this: 在清单中,您已经声明了<receiver>

android:exported="false"

That is wrong. 那是错的。 When you do that you are making your receiver private, so the Android framework cannot instantiate it when it wants to call your onReceive() during the BOOT_COMPLETED broadcast. 这样做时,您会将接收器设为私有,因此,当Android框架想要在BOOT_COMPLETED广播期间调用onReceive()时,无法实例化它。

Just remove android:exported="false" from the <receiver> tag and you should be OK. 只需从<receiver>标记中删除android:exported="false" ,您就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM