在Activity之前调用Android服务

时间:2014-11-06 08:00:12

标签: android

我正在定期向服务器发送位置服务。但是当我的应用程序启动服务时,我没有在我的主要活动中调用服务。该服务会自动调用。 提前谢谢......

我的java代码

    setContentView(R.layout.sliding);

    startService(new Intent(getBaseContext(), response.class));
      Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 40);

        Intent intent = new Intent(this, response.class);

        // Add extras to the bundle
        intent.putExtra("foo", "bar");
        // Start the service
       // startService(intent);


        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int i;
        i=15;
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                i* 1000, pintent);


        startService(intent);

我的清单文件   `                 

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

    </activity>


    <activity android:name="com.siliconicpro.sayminicab.Register"
       android:windowSoftInputMode="adjustPan"
       android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name="com.siliconicpro.sayminicab.Login"
       android:windowSoftInputMode="adjustPan"
       android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name="com.siliconicpro.sayminicab.sliding"
        android:hardwareAccelerated="true"
         android:windowSoftInputMode="adjustPan"
       android:configChanges="keyboardHidden|orientation|screenSize"
       />
    <service android:name="services.response"
        ></service>
     <service android:name="services.CallService"
        ></service>

我的服务代码是

public class response extends Service {
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private String email1;
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    Toast.makeText(getApplicationContext(), "Service Created", 1).show();
    super.onCreate();
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
//  

    Toast.makeText(getApplicationContext(), "service running", 1).show();

}         return super.onStartCommand(intent,flags,startId);

        }

1 个答案:

答案 0 :(得分:1)

startService(new Intent(getBaseContext(), response.class));

这是罪魁祸首,删除它然后尝试。