當前位置:首頁 » 安卓系統 » androidactivity傳值

androidactivity傳值

發布時間: 2022-09-08 23:01:28

㈠ android開發,請問下兩個Activity之間如何傳遞textView上的值

在startActivity的會後,通過Intent中傳值,比如

java">//啟動B
Intentbintent=newIntent(A.this,B.class);
//設置bintent的Bundle的一個值
Stringbsay=textView.getText().toString();//獲取textview的值
bintent.putExtra("listenB",bsay)
startActivity(bintent,0);

㈡ Android傳值,Activity到普通類

什麼叫普通類?在android四大組件當中,傳值普遍採用的是Intent還有Bundle這樣的形式
以Text為例
發送方是 Intent intent = new Intent(Context context,otherActivityOrService.class);
intent.putExtra("key","yourText");

startActivity(intent);//如果啟動的是service就使用startService(intent);

接收方如果是Service或是BroadcastReceiver會在生命周期方法中直接獲得intent
如果接收方仍然是一個Activity那麼應該使用Intent intent = getIntent();
然後用
String text = intent.getStringExtra("key"); 這樣的方式來獲取傳過來的值

㈢ Android 普通類中怎麼向activity中傳值

什麼叫普通類?在android四大組件當中,傳值普遍採用的是Intent還有Bundle這樣的形式
以Text為例
發送方是 Intent intent = new Intent(Context context,otherActivityOrService.class);
intent.putExtra("key","yourText");

startActivity(intent);//如果啟動的是service就使用startService(intent);

接收方如果是Service或是BroadcastReceiver會在生命周期方法中直接獲得intent
如果接收方仍然是一個Activity那麼應該使用Intent intent = getIntent();
然後用
String text = intent.getStringExtra("key"); 這樣的方式來獲取傳過來的值

㈣ android里 activity怎麼向service傳遞參數

android中activity中向service傳遞參數,有如下方法:

1.在Activity里注冊一個BroadcastReceiver,Service完成某個任務就可以發一個廣播,接收器收到廣播後通知activity做相應的操作。
2.使用bindService來關聯Service和Application,應用.apk里的所有組件一般情況都運行在同一個進程中,所以不需要用到IPC,bindService成功後,Service的Client可以得到Service返回的一個iBinder引用,具體的參見Service的文檔及onBind的例子,這樣Service的引用就可以通過返回的iBinder對象得到,如
public class LocalService extends Service {
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}

之後Client通過這個iBinder對象得到Service對象引用之後,可以直接和Service通訊,比如讀取Service中的值或是調用Service的方法。

㈤ android activity之間傳值

不要隨便使用static
雖然簡單但是很容易造成內存泄漏等問題。可以使用handle傳值,每一次主activity更新了值就發msg出來,或者用回調,或者廣播。

㈥ 【Android 開發】 : Activity之間傳遞數據的幾種方式

1. 使用Intent來傳遞數據Intent表示意圖,很多時候我們都會利用Android的Intent來在各個Activity之間傳遞數據,這也是Android比較官方的一種數據傳遞的方式需求1:從一個Activity(IntentDemo)跳轉到另外一個Activity(Other),其中利用Intent來傳遞數據程序Demo如下:IntentDemo.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentDemo extends Activity { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponent(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IntentDemo.this, Other.class); // 在Intent中傳遞數據 intent.putExtra("name", "AHuier"); intent.putExtra("age", 22); intent.putExtra("address", "XiaMen"); // 啟動Intent startActivity(intent); } }); } private void initComponent() { button = (Button) findViewById(R.id.button); } } other.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Other extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); initComponent(); Intent intent = getIntent(); int age = intent.getIntExtra("age", 0); String name = intent.getStringExtra("name"); String address = intent.getStringExtra("address"); textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address " + address); } private void initComponent() { textView = (TextView) findViewById(R.id.msg); } } ——>2. 在Activity之間使用靜態變數傳遞數據在上例中使用Intent可以很方便的在不同的Activity之間傳遞數據,這個也是官方推薦的方式,但是也有一定的局限性,就是Intent無法傳遞不能序列化的對象。我們可以使用靜態變數來解決這個問題。需求1:從一個Activity(IntentDemo)跳轉到另外一個Activity(Other),其中利用靜態變數來傳遞數據程序Demo:IntentDemo.java Intent intent = new Intent(); intent.setClass(IntentDemo.this, Other.class); Other.age = 22; Other.name = "AHuier"; Other.address = "XiaMen"; startActivity(intent); Other.java private TextView textView; public static int age; public static String name; public static String address; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); initComponent(); textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address " + address); }——>3. 通剪切板傳遞數據 在Activity之間數據傳遞還可以利用一些技巧,不管是Windows還是Linux操作系統,都會支持一種剪切板的技術,也就是一個程序將一些數據復制到剪切板上,然後其他的任何程序都可以從剪切板中獲取數據。1) 利用剪切板傳遞普通的數據,如字元串 需求1:從一個Activity(IntentDemo)跳轉到另外一個Activity(Other),通過剪切板傳遞數據 程序Demo:IntentDemo.java ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String name = "AHuier"; clipboardManager.setText(name); Intent intent = new Intent(IntentDemo.this, Other.class); startActivity(intent);Other.javaClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString(); textView.setText(msgString);——> 1) 利用剪切板傳遞復雜的數據,如對象需求1:從一個Activity(IntentDemo)跳轉到另外一個Activity(Other),通過剪切板傳遞數據新建一個MyData.javapackage com.android.intentdemo; import java.io.Serializable; public class MyData implements Serializable { private String name; private int age; public MyData(String name, int age) { super(); this.name = name; this.age = age; } // 提供一個toString()方法 @Override public String toString() { return "MyData [name=" + name + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } IntentDemo.java MyData myData = new MyData("AHuier", 22); //將對象轉換成字元串 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String base64String = ""; try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(myData); //使用Android中提供的 Base64 工具類,這個類主要是用來對對象進行壓縮也解碼的過程,使用默認方式 base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); objectOutputStream.close(); } catch (Exception e) { // TODO: handle exception } ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.setText(base64String); Intent intent = new Intent(IntentDemo.this, Other.class); startActivity(intent); Other.java ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString(); //將字元串 msgString 還原為對象 byte[] base64_byte = Base64.decode(msgString, Base64.DEFAULT); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte); try { ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); MyData myData = (MyData)objectInputStream.readObject(); textView.setText(myData.toString()); } catch (Exception e) { // TODO: handle exception }——>4.Intent中使用全局變數來傳遞數據需求1:從一個Activity(Main)跳轉到另外一個Activity(Other),通過全局變數來傳遞數據Main.javaprivate Button button; private MyApp myApp; ... myApp = (MyApp)getApplication(); myApp.setName("kunhuixu"); //修改之後的名稱 Intent intent = new Intent(Main.this, Other.class); startActivity(intent);Other.javaprivate MyApp myApp; private TextView textView; ... myApp = (MyApp)getApplication(); textView.setText("--- The app name ---" + myApp.getName());MyApp.javapackage com.android.intentglobal; import android.app.Application; /* * 查看Android官方文檔。 * Application 是所有那些需要維護全局application狀態的基類。你可以提供你自己的實現機制通過在在AndroidManifest.xml中提供你自己的需要聲明 * 的標記你自己的標簽。 * onCreate()方法是在應用程序啟動的時候被回調的。 ——>

㈦ 如何在Android開發中activity之間數據傳遞

android各組件之間可以使用Intent來傳遞數據, 這里以ActivityA向ActivityB傳遞數據為例

Intentintent=newIntent(context,ActivityB.class);

//如果context上下文不是Activity的話,需要添加下面這個flag,
if(!(contextinstanceofActivity)){
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
//添加要傳遞的數據
//這里的key到時候ActivityB會用來得到value
//android要求可以傳遞的數據類型包括基本數據類型,
//基本數據類型的數組,CharSequence,以及實現了Parcelable的對象
//putExtra是重載方法,根據value的類型會適當的打包進intent的Bundle對象里
intent.putExtra(key,value);
//或則將數據打包進Bundledata里,然後intent.putExtras(data);

context.startActivity(intent);

在ActivityB中

publicvoidonCreate(BundlesavedInstanceState){
//得到傳遞過來的數據
Bundlebundle=getIntent().getExtras();
//通過getXXX方法來獲取key對應的value
bundle.getXXX(key);
}

㈧ android activity怎麼給fragment傳值

參數傳遞方法一

在Activity中定義一個欄位、然後添加set和get方法、代碼如下、mTitle就是要傳遞的參數、如果是傳遞對象、可以把mTitle換成一個對象即可

㈨ android中activity A需要傳值給C,但是A是跳轉到B的。

在a里用startactivityforresult啟動b,退出b後a能接受到b的值,跳轉c前把b傳過來的值用intent也一起傳過去,或者不關閉a,也是用startactivityforresult啟動c,c關閉後值傳回來到a里

熱點內容
特斯拉modely買哪個配置 發布:2024-05-09 09:32:01 瀏覽:61
androidpc機 發布:2024-05-09 09:31:51 瀏覽:469
伺服器配置外網地址查詢 發布:2024-05-09 09:22:31 瀏覽:657
win系統如何組建sip伺服器 發布:2024-05-09 08:52:21 瀏覽:279
基於圖像匹配演算法 發布:2024-05-09 08:35:32 瀏覽:29
編譯安卓內核源碼 發布:2024-05-09 08:22:32 瀏覽:185
騎砍二霸主怎麼連接聯機伺服器 發布:2024-05-09 08:21:58 瀏覽:547
c語言引用參數 發布:2024-05-09 08:21:58 瀏覽:252
怎麼查看伺服器流量 發布:2024-05-09 08:12:34 瀏覽:880
不用壓縮泵 發布:2024-05-09 08:12:33 瀏覽:850