I'm trying, for the first time, to write a service within Android. I'm really new to Android programming. Here's what I am trying to do:
In the background, without alerting the user, execute the command "while yes|yes".
Problems:
- The emulator opens up a window when the app is launched
- I'm not sure if this actually does what I want because the emulator (Eclipse) refuses to run the command.
I've written, to the best of my ability, the following code using a tutorial that I found online:
MainJavaActivity.java:
package com.example.runningservice;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
startService(new Intent(this, MyService.class));
}
}
MyService.java:
package com.example.runningservice;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
public class MyService extends Service
{
Process x;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate()
{
}
public void onStart(Intent intent)
{
try {
x = Runtime.getRuntime().exec("which yes|yes");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.runningservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
Can anyone help me out?