① 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