gpt4 book ai didi

Android 服务在 Activity 之前被调用

转载 作者:行者123 更新时间:2023-11-30 02:34:37 25 4
gpt4 key购买 nike

我正在制作一项定期向服务器发送位置的服务。但是当我的应用程序启动时,服务启动了,而我没有在我的主 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com