启动Android 4.0服务启动时间(Start Android Service 4.0 boot

2019-09-20 03:15发布

我的英文程度不高。 我不能启动在系统启动时的机器人服务,我不知道这个问题。 我试图例如代码,但没有成功。 有人可以给我在Java中的一个项目在运行? 其它代码适用于其他人,但在我的平板电脑智能手机模拟器这是行不通的。 请问一个问题,Android 4.0的存在吗?

这是我的代码:

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



    }

}

Answer 1:

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

和清单文件

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


Answer 2:

我认为:

Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);

是不够的,你必须设置的意图类名这样的:

Intent i = new Intent();
i.setClassName("com.example", "com.example.MyService");
i.setAction("com.example.MyService");
context.startService(i);

请尝试。



Answer 3:

重要提示:Android操作系统3.1+仍然ignorent约在下列情况下,您的广播接收机:1.用户从来没有明确地启动应用程序至少一次。
2.用户具有“强制关闭”的应用程序。

问题已经没事做您的实现。 这是Android的一个潜在的安全漏洞,谷歌已经关闭了:)



文章来源: Start Android Service 4.0 boot time