写android程序时怎么创建一个没有界面的程序啊,我想写一个服务程序。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写android程序时怎么创建一个没有界面的程序啊,我想写一个服务程序。相关的知识,希望对你有一定的参考价值。
这个程序只需要运行几条指令,不需要输出,不需要界面。
一、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;
参考技术A 写服务程序android service(服务)本身就没有界面。
参考官网这编文章如何创建服务,有现成的例子:
http://developer.android.com/reference/android/app/Service.html追问
代码我大概知道怎么写,就是不知道怎么创建工程,我创建的工程都有界面,把OnCreate里的setContentView(R.layout.activity_main);去掉了还是有界面。
追答一打开就运行你无界面的服务,界面都不给一个啊.你可以运行服务后立马退出这个app,服务照样运行在系统中
追问我想写个一键锁屏,网上下载的都不好用。我想自己写一个不用设备管理器的版本。
以上是关于写android程序时怎么创建一个没有界面的程序啊,我想写一个服务程序。的主要内容,如果未能解决你的问题,请参考以下文章
我用VS2010写的一个C#窗体程序打包后安装,启动后有一个界面无法启动,报错啥异常文本,怎么回事啊