写android程序时怎么创建一个没有界面的程序啊,我想写一个服务程序。

这个程序只需要运行几条指令,不需要输出,不需要界面。... 这个程序只需要运行几条指令,不需要输出,不需要界面。 展开
 我来答
fengyucfa
2017-07-03
知道答主
回答量:2
采纳率:0%
帮助的人:1915
展开全部
一、
package com.example.zhuanhuan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.example.zhuanhuan.MainActivity;
import com.example.zhuanhuan.R;
import com.example.zhuanhuan.KkkActivity;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button zhuceButton = (Button) findViewById(R.id.button2);
Button dengluButton = (Button) findViewById(R.id.button1);
final EditText yonghumingEditText = (EditText) findViewById(R.id.editText1);
final EditText mimaEditText = (EditText) findViewById(R.id.editText2);

zhuceButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, KkkActivity.class);
startActivity(intent);
}
};

dengluButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {

FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory().getPath()+"/"+ yonghumingEditText.getText().toString());
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1 );
fis.close();
//Toast.makeText(getApplicationContext(), new String(input), Toast.LENGTH_SHORT).show();
String mimaString = new String(input);

if (mimaString.equals(mimaEditText.getText().toString())==true) {
Toast.makeText(getApplicationContext(), "成功登陆", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, QqqActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
二、
package com.example.zhuanhuan;

import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class QqqActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qqq);

Button fanhuiButton = (Button) findViewById(R.id.button2);
Button jianceButton = (Button) findViewById(R.id.button1);
jianceButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
Intent new = new Intent();
intent.setClass(QqqActivity.this, MainActivity.class);
startActivity(intent);
}
});
fanhuiButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(QqqActivity.this, AaaActivity.class);
startActivity(intent);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.qqq, menu);
return true;
}

}

三、
package com.example.zhuanhuan;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.example.zhuanhuan.MainActivity;
import com.example.zhuanhuan.R;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class KkkActivity extends Activity {

private File writeFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kkk);
Button backButton = (Button) findViewById(R.id.button1);
final EditText yonghumingEditText = (EditText) findViewById(R.id.editText1);
final EditText mimaEditText = (EditText) findViewById(R.id.editText2);

backButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

File writeFile = new File(Environment.getExternalStorageDirectory().getPath(), yonghumingEditText.getText().toString());
if (!writeFile.exists()) {

try {
writeFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

String abcString = mimaEditText.getText().toString();
FileOutputStream fos;

try {

for = new FileOutputStream(writeFile);
fos.write(abcString.getBytes());
fos.flush();
fos.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClass(KkkActivity.this, MainActivity.class);
startActivity(intent);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kkk, menu);
return true;
}

}
四、
package com.example.zhuanhuan;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AaaActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);

Button jianceButton = (Button) findViewById(R.id.button1);
jianceButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(AaaActivity.this, QqqActivity.class);
startActivity(intent);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.aaa, menu);
return true;
}

}
热情的梵人
推荐于2020-03-08 · TA获得超过199个赞
知道小有建树答主
回答量:216
采纳率:0%
帮助的人:99.8万
展开全部
写服务程序android service(服务)本身就没有界面。
参考官网这编文章如何创建服务,有现成的例子:
http://developer.android.com/reference/android/app/Service.html
更多追问追答
追问
代码我大概知道怎么写,就是不知道怎么创建工程,我创建的工程都有界面,把OnCreate里的setContentView(R.layout.activity_main);去掉了还是有界面。
追答
一打开就运行你无界面的服务,界面都不给一个啊.你可以运行服务后立马退出这个app,服务照样运行在系统中
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式