在后台启动应用程序,启动它以在Cellular上运行并收集数据

时间:2014-11-04 16:02:16

标签: android service background

我是Android的新手并尝试在后台启动应用程序(例如speedtest)并要求它通过单击它启动它并在后台运行并将数据结果作为输出发回。我期待所有这一切都发生在后台。我编写的代码在前台启动应用程序。

private static final String TAG = "MyService";

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {

    Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    Intent intent = getPackageManager().getLaunchIntentForPackage("org.zwanoo.android.speedtest");
    if (intent != null)
    {
        /* we found the activity now start the activity */
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    else
    {
        /* bring user to the market or let them choose an app? */
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("market://details?id="+"org.zwanoo.android.speedtest"));
        startActivity(intent);
    }


}

@Override
public void onStart(Intent intent, int startId) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");  
}

@Override
public void onDestroy() {
    Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

}

0 个答案:

没有答案