① android中service常驻
在AndroidManifest中声明Activity或者Service时,定义android:process属性
格式:android:process=":{进程名字}",这样就能运行在其他进程了
详见:http://developer.android.com/guide/topics/manifest/service-element.html
当把service跑在其他进程后,就可解决,方法参考1
按推荐做法的话,可以像Google提供的绝大多数服务那样,使用Content Provider,具体使用方式请自行搜索.另外,可以采用AIDL跟其他进程的Service直接进行通信,我们之前的做法会做一套序列/反序列化的东西在公共Service和其他普通app进行通信(当然也是通过AIDL).至于Service可以不用单独装,在你的业务app里捆绑一个小的Service也就可以了
http://segmentfault.com/q/1010000000415917
② 如何让android的service一直在后台运行
首先来说,android是不存在一直运行后台服务的。而且,后天一直运行,就会消耗很大的手机资源的,因此也会影响手机的其他程序的使用的。
需要注意的事,手机系统运行的问题,以上的方法都是建立在手机系统够大的时候才行的。要不就会被很轻易的就清理掉了。
以上就是我的回答,希望可以帮到题主。
③ Android中的Service到底起什么作用
Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 Service 是运行在主进程的 main 线程上的。如:onCreate,onStart 这些函数在被系统调用的时候都是在主进程的 main 线程上运行的。如果是Remote Service,那么对应的 Service 则是运行在独立进程的 main 线程上。因此请不要把 Service 理解成线程,它跟线程半毛钱的关系都没有!
既然这样,那么我们为什么要用 Service 呢?其实这跟 android 的系统机制有关,我们先拿 Thread 来说。Thread 的运行是独立于 Activity 的,也就是说当一个 Activity 被 finish 之后,如果你没有主动停止 Thread 或者 Thread 里的 run 方法没有执行完毕的话,Thread 也会一直执行。因此这里会出现一个问题:当 Activity 被 finish 之后,你不再持有该 Thread 的引用。另一方面,你没有办法在不同的 Activity 中对同一 Thread 进行控制。
举个例子:如果你的 Thread 需要不停地隔一段时间就要连接服务器做某种同步的话,该 Thread 需要在 Activity 没有start的时候也在运行。这个时候当你 start 一个 Activity 就没有办法在该 Activity 里面控制之前创建的 Thread。因此你便需要创建并启动一个 Service ,在 Service 里面创建、运行并控制该 Thread,这样便解决了该问题(因为任何 Activity 都可以控制同一 Service,而系统也只会创建一个对应 Service 的实例)。
因此你可以把 Service 想象成一种消息服务,而你可以在任何有 Context 的地方调用 Context.startService、Context.stopService、Context.bindService,Context.unbindService,来控制它,你也可以在 Service 里注册 BroadcastReceiver,在其他地方通过发送 broadcast 来控制它,当然这些都是 Thread 做不到的。
④ Android上开发一个长达几天的倒计时小软件, 把它做成service 还是做成能够保留在后台的Activity
很久以前写过这么个:
//TimerLabel.java
package timerlabel;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.lang.Thread.State;
/**
* 计时标签
* @author Jeky
*/
public class TimerLabel extends JLabel {
private int maxTime;
private int count;
private static final int SECOND = 1000;
private static final int FONT_SIZE = 36;
private Thread thread;
private boolean pause;
private boolean start;
/**
* 新建一个计时标签
* @param maxTime 倒计时起始时间
*/
public TimerLabel(int maxTime) {
this.setHorizontalAlignment(JLabel.CENTER);
this.setFont(new Font("Times New Roman", Font.BOLD, FONT_SIZE));
this.pause = false;
setMaxTime(maxTime);
}
/**
* 修改倒计时起始时间
* @param maxTime 新的起始时间
*/
public void setMaxTime(int maxTime) {
if (this.start) {
return;
}
this.maxTime = maxTime;
this.count = maxTime;
initText();
this.thread = new Thread(new Runnable() {
@Override
public void run() {
while (count != 0) {
try {
if (!start) {
count = 0;
initText();
break;
}
if (!pause) {
Thread.sleep(SECOND);
count--;
initText();
}
} catch (InterruptedException ex) {
pause = true;
}
}
done();
}
});
this.start = false;
}
/**
* 倒计时完成后调用此方法
*/
protected void done() {
JOptionPane.showMessageDialog(this, "Time up!");
}
/**
* 标签字符由此方法设置
*/
protected void initText() {
String min = String.valueOf(count / 60);
String sec = String.valueOf(count % 60);
while (min.length() < 2) {
min = "0" + min;
}
while (sec.length() < 2) {
sec = "0" + sec;
}
this.setText(min + ":" + sec);
}
/**
* 暂停
*/
public void pause() {
if (start) {
thread.interrupt();
}
}
/**
* 检测标签倒计时是否开始
* @return 如果开始返回true
*/
public boolean isStart() {
return start;
}
/**
* 得到倒计时起始时间
* @return 倒计时起始时间
*/
public int getMaxTime() {
return maxTime;
}
/**
* 检测标签倒计时是否暂停
* @return 倒计时暂停返回true
*/
public boolean isPause() {
return pause;
}
/**
* 从暂停中恢复计时
*/
public void continueDo() {
if (this.pause) {
this.pause = false;
}
}
/**
* 取消计时
*/
public void stop() {
if (start) {
start = false;
}
}
/**
* 开始计时
*/
public void start() {
if (thread.getState().equals(State.NEW)) {
start = true;
thread.start();
} else if (thread.getState().equals(State.TERMINATED)) {
setMaxTime(maxTime);
start = true;
thread.start();
}
}
}
//演示程序 Test.java
package timerlabel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author Jeky
*/
public class Test extends JFrame {
private TimerLabel label;
private static final int GAP = 10;
private JButton runButton;
private JButton pauseButton;
private JButton setButton;
private JButton stopButton;
private JTextField time;
public Test() {
label = new TimerLabel(10);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
runButton = new JButton("Start");
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.start();
}
});
panel.add(runButton);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.isStart()) {
if (!label.isPause()) {
label.pause();
pauseButton.setText("Continue");
} else {
label.continueDo();
pauseButton.setText("Pause");
}
}
}
});
panel.add(pauseButton);
time = new JTextField(10);
panel.add(time);
setButton = new JButton("setMaxTime");
setButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setMaxTime(Integer.parseInt(time.getText()));
}
});
panel.add(setButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.stop();
}
});
panel.add(stopButton);
this.getContentPane().add(label, BorderLayout.CENTER);
this.getContentPane().add(panel, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
试试吧
⑤ android service 怎么结束activity
方法一:
public class mService extends Service {
//保存在service中的Activity对象
private static mActivity m;
//启动服务
static void startservice(Context c){
m=(mActivity)c;
Intent iService=new Intent(c,mService.class);
iService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startService(iService);
}
//关闭服务
static void stopservice(Context c){
Intent iService=new Intent(c,mService.class);
iService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.stopService(iService);
}
……
//在mService中关闭mActivity
m.finish();
}
public class mActivity extends Activity {
// private MediaPlayer mMPlayer;
/*
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
……
//启动mService
mService.startservice(mActivity.this);
……
//停止mService
mService.stopservice(mActivity.this);
}
}
方法二:
主要包括3部分
1. 定义application类,这个类可以保存获取activity实例,记得manifest中加入android:name=".MyApp"
public class MyApp extends Application{
private MyServiceActivity myActivity;
public void setInstance(MyServiceActivity instance){
myActivity = instance;
}
public MyServiceActivity getInstance(){
return myActivity;
}
}
2. 在activity中保存实例
public class MyServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MyApp)getApplication()).setInstance(this);
……
}
}
3. 在service中取回实例
public class MyService extends Service {
MyServiceActivity myActivity;
@Override
public void onCreate() {
super.onCreate();
setForeground(true);
android.os.Debug.waitForDebugger();
myActivity = ((MyApp)getApplication()).getInstance();
……
}
}
⑥ 如何查看Android 中native的Service
在Android里面,init程序会解析 Init.rc文件,然后启动很多Native Service。如何查看这些service的状态呢,查看init的源代码,发现所有的native service的信息都会保存到系统属性里面。这样就可以用下面的命令查看各个Service的状态。
# getprop |grep init.svc
getprop |grep init.svc
[init.svc.servicemanager]: [running]
[init.svc.vold]: [running]
[init.svc.netd]: [running]
[init.svc.debuggerd]: [running]
[init.svc.omsril-daemon]: [running]
[init.svc.sdm]: [running]
[init.svc.zygote]: [running]
[init.svc.media]: [running]
[init.svc.dbus]: [running]
[init.svc.installd]: [running]
[init.svc.keystore]: [running]
[init.svc.lapisrv]: [running]
[init.svc.console]: [running]
[init.svc.tcmd-autolaunch]: [stopped]
[init.svc.tel]: [stopped]
[init.svc.pxa920-setup]: [stopped]
[init.svc.logcat]: [running]
[init.svc.logcat-radio]: [running]
[init.svc.dnsmasq]: [running]
[init.svc.powerpolicy]: [stopped]
[init.svc.adbd]: [running]
[init.svc.telserver]: [stopped]
[init.svc.bootanim]: [stopped]
[init.svc.fmradiod]: [stopped]
[init.svc.wpa_supplicant]: [running]
[init.svc.dhcpcd]: [running]
⑦ 如何让android的service一直在后台运行
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理。然后咱们注意到这个函数有一个int的返回值
从Android官方文档中,咱们知道onStartCommand有4种返回值:
START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。
START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。
现在的安卓手机,只要一长按home键,通常都会列出近期任务,这里可以干掉所有进程
所以一直不断的在后台运行是不行的,但是可以通常广播来激活自己的service