❶ android 怎麼調用service里的方法
直接傳activity 引用肯定是不行的, 最簡單的是 用靜態全局變數 ,不過不推薦。 所以只能復雜一點 ,不能service可以直接調用activity ,只能傳消息給activity ,讓activity 執行特定的方法。 就是 service start activity 。 而activity 設置為 single instance ,在newIntent 方法 裡面 處理 傳入消息。 還有一個方法就是activity 裡面定義 一個 內部類 broadcast ,然後 service 調用sendbraoadcast , broadcast 再調用 activity 方法。
❷ 怎樣在android的service中調用Activity中的getWindow函數
在Android中,Activity主要負責前台頁面的展示,Service主要負責需要長期運行的任務,所以在我們實際開發中,就會常常遇到Activity與Service之間的通信,我們一般在Activity中啟動後台Service,通過Intent來啟動,Intent中我們可以傳遞數據給Service,而當我們Service執行某些操作之後想要更新UI線程,我們應該怎麼做呢?接下來我就介紹兩種方式來實現Service與Activity之間的通信問題
通過Binder對象
當Activity通過調用bindService(Intent service, ServiceConnection conn,int flags),我們可以得到一個Service的一個對象實例,然後我們就可以訪問Service中的方法,我們還是通過一個例子來理解一下吧,一個模擬下載的小例子,帶大家理解一下通過Binder通信的方式
首先我們新建一個工程Communication,然後新建一個Service類
[java] view plain
<span style="font-family:System;">package com.example.communication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MsgService extends Service {
/**
* 進度條的最大值
*/
public static final int MAX_PROGRESS = 100;
/**
* 進度條的進度值
*/
private int progress = 0;
/**
* 增加get()方法,供Activity調用
* @return 下載進度
*/
public int getProgress() {
return progress;
}
/**
* 模擬下載任務,每秒鍾更新一次
*/
public void startDownLoad(){
new Thread(new Runnable() {
@Override
public void run() {
while(progress < MAX_PROGRESS){
progress += 5;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
/**
* 返回一個Binder對象
*/
@Override
public IBinder onBind(Intent intent) {
return new MsgBinder();
}
public class MsgBinder extends Binder{
/**
* 獲取當前Service的實例
* @return
*/
public MsgService getService(){
return MsgService.this;
}
}
}</span>
上面的代碼比較簡單,注釋也比較詳細,最基本的Service的應用了,相信你看得懂的,我們調用startDownLoad()方法來模擬下載任務,然後每秒更新一次進度,但這是在後台進行中,我們是看不到的,所以有時候我們需要他能在前台顯示下載的進度問題,所以我們接下來就用到Activity了
[java] view plain
Intent intent = new Intent("com.example.communication.MSG_ACTION");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
通過上面的代碼我們就在Activity綁定了一個Service,上面需要一個ServiceConnection對象,它是一個介面,我們這里使用了匿名內部類
[java] view plain
<span style="font-family:System;"> ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//返回一個MsgService對象
msgService = ((MsgService.MsgBinder)service).getService();
}
};</span>
在onServiceConnected(ComponentName name, IBinder service) 回調方法中,返回了一個MsgService中的Binder對象,我們可以通過getService()方法來得到一個MsgService對象,然後可以調用MsgService中的一些方法,Activity的代碼如下
[java] view plain
<span style="font-family:System;">package com.example.communication;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private MsgService msgService;
private int progress = 0;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//綁定Service
Intent intent = new Intent("com.example.communication.MSG_ACTION");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//開始下載
msgService.startDownLoad();
//監聽進度
listenProgress();
}
});
}
/**
* 監聽進度,每秒鍾獲取調用MsgService的getProgress()方法來獲取進度,更新UI
*/
public void listenProgress(){
new Thread(new Runnable() {
@Override
public void run() {
while(progress < MsgService.MAX_PROGRESS){
progress = msgService.getProgress();
mProgressBar.setProgress(progress);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//返回一個MsgService對象
msgService = ((MsgService.MsgBinder)service).getService();
}
};
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
}</span><span style="font-family: simsun;">
</span>
其實上面的代碼我還是有點疑問,就是監聽進度變化的那個方法我是直接在線程中更新UI的,不是說不能在其他線程更新UI操作嗎,可能是ProgressBar比較特殊吧,我也沒去研究它的源碼,知道的朋友可以告訴我一聲,謝謝!
上面的代碼就完成了在Service更新UI的操作,可是你發現了沒有,我們每次都要主動調用getProgress()來獲取進度值,然後隔一秒在調用一次getProgress()方法,你會不會覺得很被動呢?可不可以有一種方法當Service中進度發生變化主動通知Activity,答案是肯定的,我們可以利用回調介面實現Service的主動通知,不理解回調方法的可以看看http://blog.csdn.net/xiaanming/article/details/8703708
新建一個回調介面
[java] view plain
public interface OnProgressListener {
void onProgress(int progress);
}
MsgService的代碼有一些小小的改變,為了方便大家看懂,我還是將所有代碼貼出來
❸ android內部類
首先,注釋這兩行的寫法,沒有問題。
我猜想你的main.xml中沒有id是eat或sleep的CheckBox(或者沒有,或者強轉出錯 )
把具體的報錯貼出來,這樣才能直接找到問題,不要讓大家來猜謎,這樣定位會很快。
❹ android 中 service 如果作為 內部類 的話怎麼在配置文件中被注冊啊
內部類不可以
Service 是android的一種機制,當它運行的時候如果是Local Service,那麼對應的 Service 是運行在主進程的 main 線程上的。如:onCreate,onStart 這些函數在被系統調用的時候都是在主進程的 main 線程上運行的。如果是Remote Service,那麼對應的 Service 則是運行在獨立進程的 main 線程上。
❺ android studio 的內部類和匿名內部類有什麼關系
一、內部類: 內部類是定義在另一個類中的類,使用它的原因主要有3個: 內部類方法可以訪問該類定義所在的作用域中的數據,包括私有的數據; 內部類可以對同一個包中的其他類隱藏以來; 當想要定義一個回調函數且不想編寫大量代碼時,使用匿名內部
❻ android中service和receiver通信的問題,求指教
TestReceiver.java 中 activity 成員變數為 null,沒有賦值。
哥們你實在不行把它弄成 static 的,然後在 Activity 創建的時候初始化一下~
當然這樣代碼寫的就不敢恭維了……
如果你想讓 receiver 「通知」 Activity 執行 UI 操作,我覺得你是不是想要玩一下 MVC 啊?你可以把 Receiver 做成 Activity 的 內部類,然後使用 Java 代碼動態注冊 Receiver,這樣內部類直接可以操作外部類 Activity 的成員變數~
提示一下,如果想玩 MVC 的話建議使用 Handler 實現通知機制。
❼ android內部類怎麼使用
Android程序是調用Activity來實現界面顯示,然後再這個界面(即Activity)上創建各種控制項來顯示你想什麼樣的形式輸出界面及內容。
若想實現你說的那種「列印test」, 需要調用「TextView控制項」來實現你說的那樣的意圖。所以你的那個代碼只能在創建的JAVA項目,是可用的。
2011年
❽ 如何給Android應用創建本地服務
本文通過代碼向大家詳細介紹和演示這兩種的服務的創建過程,代碼適用於Android2.3.3以後的版本。
定義清單文件(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<!-- 本例為了方便,將啟動類型服務的Activity和綁定類型服務的Activity放到了一個類中:
LocalServiceActivities.java -->
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"android:targetSdkVersion="9"/>
<application android:icon="@drawable/icon"android:label="@string/app_name">
<!-- 本地服務LocalService -->
<service android:name="LocalService" />
<!-- 啟動類型服務的Activity,內部類Controller-->
<activity android:name=".LocalServiceActivities$Controller"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 綁定類型服務的Activity,要運行本服務,需要將下面的注釋去掉,
同時給上面的啟動類型服務的Activity給注釋掉 -->
<!--
<activity android:name=".LocalServiceActivities$Binding"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
2. 定義字元資源(strings.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LocalServiceActivity!</string>
<string name="app_name">LocalServiceApp</string>
<stringname="activity_local_service_controller">App/Service/Local Service Controller</string>
<string name="local_service_controller">This demonstrates how you can implement persistent services that
may be started and stopped as desired.</string>
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
<string name="local_service_started">Local service has started</string>
<string name="local_service_stopped">Local service has stopped</string>
<string name="local_service_label">Sample Local Service</string>
<stringname="activity_local_service_binding">App/Service/Local Service Binding</string>
<string name="local_service_binding">This demonstrates how you can connect with a persistent
service. Notice how it automatically starts for you, and play around with the
interaction between this and Local Service Controller.</string>
<string name="bind_service">Bind Service</string>
<string name="unbind_service">Unbind Service</string>
<string name="local_service_connected">Connected to local service</string>
<string name="local_service_disconnected">Disconnected from local service</string>
</resources>
3. 定義布局資源
3.1. local_service_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/local_service_controller"/>
<Button android:id="@+id/start"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:text="@string/start_service">
<requestFocus />
</Button>
<Button android:id="@+id/stop"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:text="@string/stop_service">
</Button>
</LinearLayout>
❾ android 內部類的問題
把class前面那個static去掉
❿ android activity 調用service 怎麼寫
直接傳activity 引用肯定是不行的,最簡單的是 用靜態全局變數 ,不過不推薦。所以只能復雜一點 ,不能service可以直接調用activity ,只能傳消息給activity ,讓activity 執行特定的方法。就是 service start activity 。而activity 設置為 single instance ,在newIntent 方法 裡面 處理 傳入消息。還有一個方法就是activity 裡面定義 一個 內部類 broadcast ,然後 service 調用sendbraoadcast ,broadcast 再調用 activity 方法。