启动服务时如何运行MainActivity?

时间:2015-11-26 18:48:38

标签: java android android-studio service

我在Java / Android Studio中很新(并且刚刚在这里创建了一个帐户),如果我说/写错了,请提前道歉。

所以我有以下问题,我有这个MainActivity.java(只是下面代码的一部分):

public class MainActivity extends Activity implements LocationListener{

public static final MediaType FORM_DATA_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
//URL derived from form URL
public static final String URL = "...";
//input element ids found from the live form page
public static final String LAT="entry_1903202747";
public static final String LONG="entry_1549783406";
public static final String MOD="entry_1161588883";
public static final String MAN="entry_852582911";
public static final String IMEI="entry_738564261";
public static final String EXIP="entry_1256979126";
public static final String ACC="entry_1601482207";

private Context context;

LocationManager locationmanager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);

    context=this;

    Button button3 = (Button)findViewById(R.id.button3);

    final TextView textView3 = (TextView) findViewById(R.id.textView3);
    final TextView textView5 = (TextView) findViewById(R.id.textView5);
    final TextView textView12 = (TextView) findViewById(R.id.textView12);
    final TextView textView13 = (TextView) findViewById(R.id.textView13);
    final TextView textView14 = (TextView) findViewById(R.id.textView14);
    final TextView textView7 = (TextView) findViewById(R.id.textView7);
    final TextView textView15 = (TextView) findViewById(R.id.textView15);


    button3.setOnClickListener(new View.OnClickListener(){

        @Override
                public void onClick(View v) {
                PostDataTask postDataTask = new PostDataTask();

            postDataTask.execute(URL,textView3.getText().toString(),textView5.getText().toString(),textView12.getText().toString(),textView13.getText().toString(),textView14.getText().toString(),textView7.getText().toString(),textView15.getText().toString());

        }
    });

我有一个MyService.java:

public class MyService extends Service
{

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

*编辑

当按下“提交”按钮时,当前应用程序将数据发送到Google表单,但我希望应用程序在后台运行时发送数据,而不按“提交”按钮。

因此,当我开始活动时(仅在“开始活动按钮”上按一次),它需要开始向表单发送数据。希望这可以解决问题,如果没有,我会尝试进一步解释它。

我想要这样的东西(MyService.java):

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();

    button3.setOnClickListener(new View.OnClickListener(){

        @Override
                public void onClick(View v) {
                PostDataTask postDataTask = new PostDataTask();

            postDataTask.execute(URL,textView3.getText().toString(),textView5.getText().toString(),textView12.getText().toString(),textView13.getText().toString(),textView14.getText().toString(),textView7.getText().toString(),textView15.getText().toString());

        }
    });

    return START_STICKY;
}

主要目标是在MyService中执行“postDataTask”(来自MainAcitivy)

1 个答案:

答案 0 :(得分:2)

我可能会延伸到intentService,因此它可以在另一个线程上发送数据,因此您不会放慢UI的速度。但是,如果您需要该服务,那么您的方法是可以的,但我不清楚您的目标是什么。无论如何,你的确切问题是

  

启动服务时如何运行MainActivity?

。服务是一种上下文,因此您可以从中启动活动。这样做:

@Override 
public int onStartCommand(Intent intent, int flags, int startId)
{ 
     Intent intent = new Intent(this, DisplayMessageActivity.class);
    startActivity(intent);

}