-3

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:

  1. The emulator opens up a window when the app is launched
  2. 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?

TRAMZ_11
  • 31
  • 1
  • 6

2 Answers2

0

First of all, I understand that the goal for this project is to start a service in the background without starting an activity. To do this you have a few options.

  1. Create a custom Application class and call startService() from there. Link to example.

  2. Create a broadcastReciever, which gets called when certain events occur, for example you can create a broadcastReciever that gets called when the phone is turned on. Link to example.

Community
  • 1
  • 1
mrunia
  • 180
  • 8
0

I would also recommend checking out this guy's github for a good example of a fork bomb. Even though he uses an activity to launch it you could modify it to run in a service.

mrunia
  • 180
  • 8