安卓。重新启动我的广播接收器。不工作。

发布于 2024-12-27 22:43:06 字数 542 浏览 1 评论 0原文

当手机开机时,如何使用嵌套类在android中启动服务?

我的包包含嵌套类。

包名称

com.android

  1. MainActivity
  2. BroadCastReceiver

我正在尝试重新启动我的 BroadCast 接收器。但它不起作用(失败)。我不知道原因是否是由于嵌套类的任何问题。

 <receiver android:name="ConnectionReceiver"></receiver> 

<receiver android:name="com.android.MainActivity .BroadCastReceiver "> 
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED" /> 
</intent-filter> 
</receiver> 

How to start a service in android using nested class when mobile is switched ON?

I have the package contains nested class.

package Name

com.android

  1. MainActivity
  2. BroadCastReceiver

I am trying to reboot my BroadCast receiver. But it doesn't work (getting Failed). I don't know if the reason is, because of any problems for nested classes.

 <receiver android:name="ConnectionReceiver"></receiver> 

<receiver android:name="com.android.MainActivity .BroadCastReceiver "> 
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED" /> 
</intent-filter> 
</receiver> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2025-01-03 22:43:06

清单文件

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

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

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

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

        <service 
            android:name=".Onchip_BackgroundService">
            <intent-filter >
                <action android:name="onchip.automobile.caraccessory1.BACKGROUND_SERVICE" />                
            </intent-filter>            
        </service>
    </application>

</manifest>

Onchip_BroadcastReceiver.java

package onchip.automobile.caraccessory1;

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


public class Onchip_BroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent serviceIntent = new Intent("onchip.automobile.caraccessory1.BACKGROUND_SERVICE");        
        context.startService(serviceIntent);
    }   
}

Onchip_BackgroundService.java

package onchip.automobile.caraccessory1;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;


public class Onchip_BackgroundService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Onchip_BackgroundService Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "Onchip_BackgroundService Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Onchip_BackgroundService Destroyed", Toast.LENGTH_LONG).show();
    }      

}

manifest file

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

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

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

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

        <service 
            android:name=".Onchip_BackgroundService">
            <intent-filter >
                <action android:name="onchip.automobile.caraccessory1.BACKGROUND_SERVICE" />                
            </intent-filter>            
        </service>
    </application>

</manifest>

Onchip_BroadcastReceiver.java

package onchip.automobile.caraccessory1;

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


public class Onchip_BroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent serviceIntent = new Intent("onchip.automobile.caraccessory1.BACKGROUND_SERVICE");        
        context.startService(serviceIntent);
    }   
}

Onchip_BackgroundService.java

package onchip.automobile.caraccessory1;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;


public class Onchip_BackgroundService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Onchip_BackgroundService Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "Onchip_BackgroundService Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Onchip_BackgroundService Destroyed", Toast.LENGTH_LONG).show();
    }      

}
岁月染过的梦 2025-01-03 22:43:06

R u 将 BroadcastReceiver 作为嵌套类。如果是,请不要这样做。

1)如果您将接收器作为嵌套类,请在活动中动态注册它,不要在清单中注册

2)如果您在清单中注册它,请为接收器创建单独的类。

R u having BroadcastReceiver as nested class. if yes dont do this.

1)if you have receiver as nested class, register it in activity dynamically, do not register in manifest

2) if you are registering it in manifest , make separate class for Receiver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文