Android 服务在 Activity 之前被调用

标签 android

我正在制作一项定期向服务器发送位置的服务。但是当我的应用程序启动时,服务启动了,而我没有在我的主 Activity 中调用服务。该服务被自动调用。 提前致谢...

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

我的 list 文件 `

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

} 返回 super.onStartCommand(intent, flags, startId);

        }

最佳答案

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

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

关于Android 服务在 Activity 之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774403/

相关文章:

android - 如何在 Android 5 中将 ImageView 放在 Button 前面?

android - 如何在Windows 7中安装Winre

android - 尝试在空对象引用(Fragment)上调用虚拟方法 'android.content.res.Resources$Theme android.content.Context.getTheme()'

android - 如何以编程方式获取自己的 Android 应用统计信息?

android - 为什么要在各个android组件中使用字符串资源- "android:text"属性?

android - 如何在Android中收集多个状态流

android - fragment 能否在旋转时从 Dialog 切换到 Embedded?

Android 谷歌地图 setMyLocationEnabled(true)

java - animateLayoutChanges 单独对元素进行动画处理,即使它们位于 ViewGroup 中

android - 带有自定义 View 的对话框,setCanceledOnTouchOutside 不起作用