开始的Android 4.0服务启动时间 [英] Start Android Service 4.0 boot time
本文介绍了开始的Android 4.0服务启动时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的英语很差。我无法启动在系统启动时的机器人服务,我不知道这个问题。我试图例如codeS,但没有成功。有人可以给我在Java中的一个项目在运行?其他code适用于其他人,但在我的平板电脑智能手机模拟器这是行不通的。请问一个问题,Android 4.0的存在?
这是我的code:
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.service22
安卓版code =1
机器人:=的versionName1.0
安卓的installLocation =internalOnly
> <支持屏安卓largeScreens =false的机器人:normalScreens =真正的机器人:smallScreens =FALSE/>
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/> <应用机器人:图标=@绘制/ ic_launcher机器人:标签=@字符串/ APP_NAME>
<服务机器人:名字=com.example.MyService>
&所述;意图滤光器>
<作用机器人:名字=com.example.MyService>
< /作用>
&所述; /意图滤光器>
< /服务>
<接收机器人:名字=com.example.MyReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED>
< /作用>
<类机器人:名字=android.intent.category.HOME>
< /类别>
&所述; /意图滤光器>
< /接收器>
< /用途>< /清单> 公共类为MyService扩展服务
{ 私有静态最后弦乐LOG_TAG =::监视;
@覆盖
公共无效的onCreate(){
super.onCreate();
Log.e(LOG_TAG,服务创造。);
} @覆盖
公共无效调用onStart(意向意图,诠释startId)
{
super.onStart(意向,startId); 的for(int i = 0; I< 20;我++)
{
mensaje();
} Log.e(LOG_TAG,服务启动。);
}
@覆盖
公共无效的onDestroy()
{
super.onDestroy();
Log.e(LOG_TAG,服务破坏。);
} @覆盖
公众的IBinder onBind(意向意图)
{
Log.e(LOG_TAG,服务绑定。);
返回null;
} 公共无效mensaje()
{
Toast.makeText(这一点,霍拉,Toast.LENGTH_LONG).show();
}} 公共类MyReceiver扩展广播接收器
{
公共MyReceiver()
{
} 字符串LOG_TAG =:: StartAtBootServiceReceiver @覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{ Log.e(LOG_TAG的onReceive:);
如果(intent.getAction()。等于(Intent.ACTION_BOOT_COMPLETED)){
意图I =新意图();
i.setAction(com.example.MyService);
context.startService(ⅰ);
} }}
解决方案
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;公共类OnBootReceiver扩展广播接收器{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.d(OnBootReceiver,妈妈!);
}
}
和清单文件
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
<应用机器人:图标=@绘制/ CW
机器人:标签=@字符串/ APP_NAME>
<接收机器人:OnBootReceiverNAME =>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /接收器>
< /用途>
My English is poor. I cannot start an android service in a boot time and I do not know the problem. I was trying example codes, but without success. Can somebody send me a project in Java that runs? Other code works for other people but on my tablet smartphone emulator it does not work. Does a problem exist in android 4.0?
This is my code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service22"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
>
<supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<service android:name="com.example.MyService">
<intent-filter>
<action android:name="com.example.MyService">
</action>
</intent-filter>
</service>
<receiver android:name="com.example.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
public class MyService extends Service
{
private static final String LOG_TAG = "::Monitor";
@Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
for (int i = 0 ; i < 20 ; i++)
{
mensaje();
}
Log.e(LOG_TAG, "Service started.");
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
@Override
public IBinder onBind(Intent intent)
{
Log.e(LOG_TAG, "Service bind.");
return null;
}
public void mensaje()
{
Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show();
}
}
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
}
String LOG_TAG = "::StartAtBootServiceReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
}
}
}
解决方案
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("OnBootReceiver", "Hi, Mom!");
}
}
and manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/cw"
android:label="@string/app_name">
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
这篇关于开始的Android 4.0服务启动时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文