Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android服务执行一个命令_Java_Android_Shell_Service_Command - Fatal编程技术网

Java Android服务执行一个命令

Java Android服务执行一个命令,java,android,shell,service,command,Java,Android,Shell,Service,Command,我第一次尝试在Android中编写服务。我对Android编程真的很陌生。以下是我正在尝试做的: 在后台,在不通知用户的情况下,在yes | yes时执行命令 问题: 当应用程序启动时,模拟器会打开一个窗口 我不确定这是否真的符合我的要求,因为emulator Eclipse拒绝运行该命令。 我已经尽我所能,使用我在网上找到的教程编写了以下代码: MainJavaActivity.java: package com.example.runningservice; import java.io.

我第一次尝试在Android中编写服务。我对Android编程真的很陌生。以下是我正在尝试做的:

在后台,在不通知用户的情况下,在yes | yes时执行命令

问题:

当应用程序启动时,模拟器会打开一个窗口 我不确定这是否真的符合我的要求,因为emulator Eclipse拒绝运行该命令。 我已经尽我所能,使用我在网上找到的教程编写了以下代码:

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>

有人能帮我吗?

首先,我知道这个项目的目标是在后台启动服务,而不启动活动。要做到这一点,你有几个选择

创建一个自定义应用程序类并从中调用startService

创建一个BroadcastReceiver,当某些事件发生时会调用它,例如,您可以创建一个BroadcastReceiver,当手机开机时会调用它


我还建议大家看看一个很好的叉子炸弹的例子。即使他使用一个活动来启动它,你也可以将它修改为在服务中运行。

MyService.java在哪里?很抱歉,这篇文章的大部分内容似乎都被切断了。