1

我想在启动时启动服务。我已经完成了一个广播接收器,从广播接收器启动了一个服务,所有这些都插入到了 xml 中。来自的教程http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/对我帮助很大。结果:在模拟器上,就像在真实设备上发生的那样: - 服务启动,然后立即停止!我不知道为什么服务停止!

该服务肯定已启动,我可以读取日志和 toast msg。证明服务已启动。

同样在日志中,我可以看到一些消息,例如:

<u>
05-17 23:18:44.379: D/EAS SyncManager(247): !!! EAS SyncManager, onCreate
05-17 23:18:44.789: D/EAS SyncManager(247): !!! EAS SyncManager, onStartCommand
05-17 23:18:44.860: D/EAS SyncManager(247): !!! EAS SyncManager, stopping self
05-17 23:18:44.999: D/My Service(258): starting broadcast receiver
05-17 23:18:45.119: D/My Service(258): starting service in onCreate
05-17 23:18:45.985: D/Eas Debug(247): Logging: 
05-17 23:18:45.999: D/EAS SyncManager(247): !!! EAS SyncManager, onDestroy
05-17 23:18:46.299: I//system/bin/fsck_msdos(29): Attempting to allocate 998 KB for FAT
</u>

EAS SyncManager 被销毁。我相信这就是我的服务被迫停止的原因。从我从http://hi-android.info/src/com/android/exchange/SyncManager.java.html读到的内容来看,当 EAS SyncManager 被销毁时,启动时启动的服务会收到一个 StopSelf()默认。

我不知道这是不是真的。如果它是真的......我该如何解决它?直到现在还有人反对这个吗?请帮忙。

这是代码:

// 服务等级

public class Contacts_Service extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();   
        Log.d("My Service", "onCreate");

        mPeriodicEventHandler = new Handler();      
                mPeriodicEventHandler.postDelayed(doPeriodicTask,13000);
    }



     private Runnable doPeriodicTask = new Runnable()
        {
            public void run() 
            {

                //your action here
                Log.d("My Service", "eeeevery 13000 ms");



                mPeriodicEventHandler.postDelayed(doPeriodicTask, 13000);
            }
        };

}

// BroadcastReceiver 类在启动时启动服务

public class MyStartupIntentReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

         if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){

         Log.d("My Service", "starting broadcast receiver");
            Intent serviceIntent = new Intent();
            serviceIntent.setAction("com.Srv.mail.Contacts_Service");
            context.startService(serviceIntent);

            //serviceIntent.setAction("com.Srv.mail.SrvMailSettingsActivity");
            //context.startActivity(serviceIntent);

    }

    }

}

还有 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Srv.mail"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />



<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >


 <receiver android:name=".MyStartupIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>


        <service android:enabled="true" android:name="Contacts_Service">
                <intent-filter>
                 <action android:name="com.Srv.mail.Contacts_Service" />
            </intent-filter>
            </service>  






        <activity
            android:name="com.Srv.mail.SrvMailSettingsActivity"
            android:label="@string/app_name" >
            <!-->intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter-->

        </activity>

    </application>

</manifest>
4

1 回答 1

0

我不确定也试试这个..也许它有效

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// Your code
}
于 2012-05-19T11:33:48.637 回答