【问题标题】:Android Service is Called before ActivityAndroid 服务在 Activity 之前被调用
【发布时间】:2014-11-06 08:00:11
【问题描述】:

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

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

        }

【问题讨论】:

  • 这是我的电话号码
  • 你能用 AndroidManifest.xml 发布 java 文件吗
  • Pratik Butani 我的代码在上面给出
  • @ツPratikButaniツ 现在清单和代码已给出

标签: android


【解决方案1】:
startService(new Intent(getBaseContext(), response.class));

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

【讨论】:

  • @Anuj Sharma 它不工作。我很高兴检查它,但是当我的应用程序运行服务 onstartcommand() 方法被调用,并且我的 oncreate() 方法被调用按钮单击时。
  • 服务也没有在 stopService(new Intent(myclass.this,CallService.class)); 上停止
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多