本人是android开发开发新手,最近在做一个项目,在一个Activity中调用startService时返回null,要调用的service跟这个Activity不在同一个包中;如果把这个service放入这个Activity所在的包中那么调用就能成功。在Manifest文件中有各个Activity个Service的声明。请问各位大侠这是什么原因造成的?

解决方案 »

  1.   

    在Activity中:Intent intent = new Intent(this, LocalService.class);
        
         ComponentName svr = startService(intent);
         if( svr == null)
         {
         Log.i("startService", "fail");
         }在Manifest文件中:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0" package="com.news_viewer.activity">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name="com.news_viewer.activity.StartUpActivity"
                      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.service.RSSDownLoadService" />
            <service android:name="com.service.LocalService" />    </application>
    </manifest>
      

  2.   

    在不同包里面 activity之间是不能用intent跳转到,可能service 也不行。
      

  3.   


    那要用什么,PendingIntent吗?
      

  4.   

    Intent intent = new Intent(this, com.service.LocalService.class);
    这样试试
      

  5.   

    http://xiaofancn.iteye.com/blog/1000585
      

  6.   

    跨进程调用别的service,需要用AIDL吧。
      

  7.   

    可以通过broadcast实现,在你activity里通过intent(action);在服务的类里面加上intet-filter。能够实现的
      

  8.   

    你在manifest里的package属性里写上公共包名com.news_viewer,在后面定义的Activity或者Service标签上写上.类名就行,在程序里import 自己的R就行。
      

  9.   

    这个帧可以,Intent intent = new Intent();  
     第一个参数为 AndroidMinifest.xml文件中配置的package属性,第二个参数为要调转的package  + class  
    intent.setClassName("com.android.activity", "com.android.activity.image.ImageActivity");  
    his.startActivity(intent);
    ------
    Intent intent = new Intent(this, LocalService.class);这样子只适合 同一个package的情况
      

  10.   

    这个问题已经解决,可能是我在Manefest.xml文件中把包名写错了。
    前一段时间比较忙,没来得及结贴;现在结贴,谢谢大家的积极参与,来者有份!!!