2012-07-24 86 views
0

我的英文很差。我无法在启动时启动安卓服务,但我不知道问题所在。我正在尝试示例代码,但没有成功。有人可以给我一个运行Java的项目吗?其他代码适用于其他人,但在我的平板电脑智能手机模拟器上不起作用。 Android 4.0中是否存在问题?开始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); 
     } 



    } 

} 
+0

[在启动时Android 4.0上启动Android服务]的可能重复(http://stackoverflow.com/questions/11640142/start-android-service-on -boot-time-android-4-0) – CommonsWare 2012-07-24 23:44:01

+0

[尝试在Android上启动服务时启动服务](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on -boot-on-android) – user3439968 2014-09-24 21:17:21

回答

3
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> 
+0

这是不可能的。它不工作,我已经做你的解决方案,但没有工作。有人可以发送给我一个android proyect白衣这个?电子邮件:[email protected],我很沮丧。谢谢。 – TSW1985 2012-07-25 10:06:44

+1

@ user1549952:“它不工作” - 是的,它的确如此。但是,在Android 3.1+上,必须在任何'BroadcastReceiver'工作之前手动运行其中一个组件。这就是为什么[采用这些代码片段的完整示例项目](https://github.com/commonsguy/cw-omnibus/tree/master/SystemEvents/OnBoot)有一个供用户启动的活动。然后,您的服务将控制所有后续重新启动,或者直到用户在设置应用程序中按下强制停止(在这种情况下,您的应用程序需要在接收器工作之前再次启动手动组件)。 – CommonsWare 2012-07-26 13:14:48

0

我想:

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

请试试看。

0

重要提示:在以下情况下,Android OS 3.1+仍会忽略您的广播接收器: 1.用户从未明确启动过应用程序至少一次。
2.用户已经“强制关闭”应用程序。

问题与您的实施无关。这是谷歌已关闭的Android中的一个潜在安全漏洞:)