Android系统。重新启动我的广播接收器。不工作。 [英] Android. Reboot my BroadCast receiver. Not working.
本文介绍了Android系统。重新启动我的广播接收器。不工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用嵌套类开始在android系统的服务时,移动切换到ON?
我的包中包含嵌套类。
包名称
com.android
- MainActivity
- 广播接收器
我试图重新启动我的广播接收器。但它不工作(获得失败)。我不知道的原因是因为对于嵌套类的任何问题。
<接收机器人:名字=ConnectionReceiver>< /接收器><接收机器人:名字=com.android.MainActivity .BroadCastReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /接收器>
解决方案
清单文件
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=onchip.automobile.caraccessory1
安卓版code =1
机器人:=的versionName1.0> <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/> <应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME> <接收
机器人:Onchip_BroadcastReceiverNAME =>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
<类机器人:名字=android.intent.category.HOME/>
&所述; /意图滤光器>
< /接收器> <服务
机器人:名字=Onchip_BackgroundService。>
&所述;意图滤光器>
<作用机器人:名字=onchip.automobile.caraccessory1.BACKGROUND_SERVICE/>
&所述; /意图滤光器>
< /服务>
< /用途>< /清单>
Onchip_BroadcastReceiver.java
包onchip.automobile.caraccessory1;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
Onchip_BroadcastReceiver扩展广播接收器{public类 @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根
意图serviceIntent =新意图(onchip.automobile.caraccessory1.BACKGROUND_SERVICE);
context.startService(serviceIntent);
}
}
Onchip_BackgroundService.java
包onchip.automobile.caraccessory1;进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.widget.Toast;
公共类Onchip_BackgroundService延伸服务{ @覆盖
公众的IBinder onBind(意向意图){
返回null;
} @覆盖
公共无效的onCreate(){
super.onCreate();
Toast.makeText(这一点,Onchip_BackgroundService创建Toast.LENGTH_LONG).show(); } @覆盖
公共无效调用onStart(意向意图,诠释startId){
super.onStart(意向,startId);
Toast.makeText(这一点,Onchip_BackgroundService开始,Toast.LENGTH_LONG).show();
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
Toast.makeText(这一点,Onchip_BackgroundService毁,Toast.LENGTH_LONG).show();
}}
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
- MainActivity
- 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>
解决方案
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();
}
}
这篇关于Android系统。重新启动我的广播接收器。不工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文