Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android 4.0.3上启动服务_Android - Fatal编程技术网

如何在android 4.0.3上启动服务

如何在android 4.0.3上启动服务,android,Android,我不想在android 4.0.3启动时启动服务,但当我启动设备时,它不会在我的设备中发生,服务也不会启动。请帮助我,谢谢 这是我的舱单 <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@stri

我不想在android 4.0.3启动时启动服务,但当我启动设备时,它不会在我的设备中发生,服务也不会启动。请帮助我,谢谢

这是我的舱单

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".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:name="com.android.testsharedpreference.MyService"></service>

    <receiver android:name="com.android.testsharedpreference.BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" >
            </action>
        </intent-filter>
    </receiver>
</application>
还有我的服务

     public class MyService extends Service
     {
          @Override
          public IBinder onBind(Intent intent) 
          {
             return null;
          }

          @Override
          public void onCreate() 
          {
               super.onCreate();
              Toast.makeText(MyService.this, "onCreate Service ",                 Toast.LENGTH_LONG).show();
          }

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

您应该添加
添加android.permission.RECEIVE\u BOOT\u COMPLETED
,因为ICS之前不需要它,所以应用程序工作正常,但从ICS开始,您没有此权限。您的应用程序将不会收到引导完成的意图

不要忘记覆盖MyService中的方法“onStartCommand”:

    @Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    String action = intent == null ? null : intent.getAction();
    Log.d("onStartCommand", "action = " + action);
    /* ... */
    return START_NOT_STICKY; // for example ...
}

你的主要活动开始了吗?应用程序在启动之前不会注册接收者。我启动了我的主活动,但如何在主活动上注册接收者ICS上新安装的应用程序在应用程序运行(处于“停止”状态)之前不会安装您在清单中定义的
接收者。除了运行应用程序,你不需要做任何事情。请参阅“非常感谢”,但如果我无法设置意图,我如何在代码中添加flag flag\u INCLUDE\u STOPPED\u包。操作\u BOOT\u已完成如何实现在启动时启动服务?
    @Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    String action = intent == null ? null : intent.getAction();
    Log.d("onStartCommand", "action = " + action);
    /* ... */
    return START_NOT_STICKY; // for example ...
}