我的配置如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_sync.service" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="ServiceStartReceiver">
<intent-filter>
<action android:name="NotifyServiceStart"></action>
</intent-filter>
</receiver> <receiver android:name="ServiceStopReceiver">
<intent-filter>
<action android:name="NotifyServiceStop"></action>
</intent-filter>
</receiver> <service android:name="AndroidSyncService">
<intent-filter>
<action android:name="SyncService"></action>
</intent-filter>
</service>
</application>
</manifest>public class AndroidSyncService extends Service{
public IBinder onBind(Intent intent) {
return null;
} public void onCreate() {
Log.d("TAG", "onCreate22222222222222-----------------");
}
public void onDestroy() {
Log.d("TAG", "onDestroy222222222222-----------------");
stopSocket();
} public void onStart(Intent intent, int startId) {
Log.d("TAG", "onStart222222222222-----------------");
startSocket();
}
}
public class ServiceStartReceiver extends BroadcastReceiver {
// private static String SERVICE_START = "NotifyServiceStart";
private static String TAG = "____sync";
private static String SYNC_SERVICE = "SyncService"; @Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "service start!--------");
Intent service = new Intent(SYNC_SERVICE);
context.startService(service);
}
}
public class ServiceStopReceiver extends BroadcastReceiver {
// private static String SERVICE_STOP = "NotifyServiceStop";
private static String TAG = "____sync";
private static String SYNC_SERVICE = "SyncService"; @Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "service stop!--------");
Intent service = new Intent(SYNC_SERVICE);
context.stopService(service);
}
}在命令行下发送广播:
adb shell am broadcast -a NotifyServiceStart
adb shell am broadcast -a NotifyServiceStopLog打印了,证明程序运行了.但BroadcastReceiver和Service里面的断点无效..
如果添加一个activity的话,BroadcastReceiver和Service里面的断点有效....为什么啊????        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

解决方案 »

  1.   

    因为receiver和service是后台操作的并且还是无界面的,你断点搞不到那里去
      

  2.   

    用DDMS 在你的进程那 放虫子
      

  3.   

    没有 Activity 就相当于没有主进程,当然就无法调试了,
    可以放一个 AndroidTestCase 类来调试
    1 配置 AndroidManifest.xml 文件的 instrumentation 节,添加 android.test.InstrumentationTestRunner 及相应包名。
    2 在 Application 节,添加 Uses Library : android.test.runner.
    3 新建从 AndroidTestCase 继承的类,并重载 runTest 方法加入测试代码。
      另外,请在 setUp 中初始化,tearDown 中释放对象。
    4 右击项目,选择 Run As -> Android JUnit Test 即可。
    5 如需 Context,请重载 setContext 方法并保存 Context。