android的页面跳转时,如A->B,这时候在B页面怎么获取A的类名

android的页面跳转时,如A->B,这时候在B页面怎么获取A的类名,第1张

android页面跳转时,从a界面跳转到b界面,获取a的类名的方式,可以使用intent意图总的bundle将类名放入key-value键值对中,在b页面进行读取,如下代码:

假设A界面跳到B界面

//FirstActivity中:

Intent mIntent = new Intent(this,SecondActivityclass);

mIntentputExtra("name","FirstActivity")

startActivity(mIntent);

//B界面中

String activityName = getIntent()getExtra("name");

1、在实际应用中,我们不仅要向Activity中传数据,也要从Activity中返回数据。虽然传递数据和返回数据类似,也可以采用前面四篇中提到的4种方法,但是一般建议采用Intent对象的方式来返回数据,使用这种方式返回数据,需要使用startActivityForResult方法来显示Activity;

2、新建Android项目“android_intent_forresult”,打开布局文件“activity_mainxml”,添加“LinearLayout”、“TextView”、“EditView”等标签,代码如下:

<LinearLayout xmlns:android=">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<EditText

android:id="@+id/one"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" + " >

</TextView>

<EditText

android:id="@+id/two"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" = " >

</TextView>

<EditText

android:id="@+id/result"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

</LinearLayout>

<Button

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="计算结果" >

</Button>

</LinearLayout>

3、新建布局文件“otherxml”,添加“TextView”、“EditView”、“Button”标签,代码如下:

<xml version="10" encoding="utf-8">

<LinearLayout xmlns:android=">

4、新建“OtherActivityjava”文件,并使其继承“Activity”,添加“onCreate”方法,代码如下:package comandroidmyintent;

import androidappActivity;

import androidosBundle;

public class OtherActivity extends Activity {

 @Override

 protected void onCreate(Bundle savedInstanceState) {

   // TODO Auto-generated method stub

   superonCreate(savedInstanceState);

   setContentView(Rlayoutother);

 }

}

5、在“AndroidManifestxml”清单文件中加入“Activity”,加入代码:<activity android:name="OtherActivity" >

       </activity>

6、在“Mainjava”中添加Button成员和“setOnClickListener”,实现两个Button的跳转,点击第一个Activity后,出现第二个Activity;在此方法内部创建意图,用“startActivityForResult”启动意图,并在Main类里重写“onActivityResult”;添加“EditText”成员,实现数据的输入并传入Intent中。代码如下:

package comandroidmyintent;

import androidRinteger;

import androidosBundle;

import androidappActivity;

import androidcontentIntent;

import androidviewMenu;

import androidviewView;

import androidwidgetButton;

import androidwidgetEditText;

public class Main extends Activity {

 private Button button;

 private final static int REQUESTCODE = 1;// 表示返回的结果码

 private EditText one, two, result; // 数据输入

 @Override

 protected void onCreate(Bundle savedInstanceState) {

   superonCreate(savedInstanceState);

   setContentView(Rlayoutactivity_main);

   one = (EditText) thisfindViewById(Ridone);

   two = (EditText) thisfindViewById(Ridtwo);

   result = (EditText) thisfindViewById(Ridresult);

   button = (Button) thisfindViewById(Ridbutton);

   buttonsetOnClickListener(new ViewOnClickListener() {

     @Override

     public void onClick(View arg0) {

       // TODO Auto-generated method stub

       // 点击后获得用户录入的值

       int a = IntegerparseInt(onegetText()toString());

       int b = IntegerparseInt(twogetText()toString());

       // 创建意图

       Intent intent = new Intent(Mainthis, OtherActivityclass);

       // 将值传入意图

       intentputExtra("a", a);

       intentputExtra("b", b);

       startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果

     }

   });

 }

 // 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。

 @Override

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   // TODO Auto-generated method stub

   superonActivityResult(requestCode, resultCode, data);

 }

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

   // Inflate the menu; this adds items to the action bar if it is present

   getMenuInflater()inflate(Rmenumain, menu);

   return true;

 }

}

7、在“OtherActivityjava”文件中添加Button和TextView成员,获取意图中的数据,代码如下:package comandroidmyintent;

import androidappActivity;

import androidcontentIntent;

import androidosBundle;

import androidwidgetButton;

import androidwidgetTextView;

public class OtherActivity extends Activity {

 private Button button;

 private TextView textView;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

   // TODO Auto-generated method stub

   superonCreate(savedInstanceState);

   setContentView(Rlayoutother);

   // 实例化button和textview

   button = (Button) thisfindViewById(Ridbutton2);

   textView = (TextView) thisfindViewById(Ridmsg);

   Intent intent = getIntent(); // 获取Intent

   // 取出Intent中的值

   int a = intentgetIntExtra("a", 0);

   int b = intentgetIntExtra("b", 0);

   textViewsetText(a + " + " + b + " = " + " ");

 }

}

运行一下,看下效果:

点击“计算结果”,跳转到第二个Activity:

8、回到“Mainjava”文件中,从OtherActivity中获取数据并显示,代码如下:

package comandroidmyintent;

import androidRinteger;

import androidosBundle;

import androidappActivity;

import androidcontentIntent;

import androidviewMenu;

import androidviewView;

import androidwidgetButton;

import androidwidgetEditText;

public class Main extends Activity {

 private Button button;

 private final static int REQUESTCODE = 1;// 表示返回的结果码

 private EditText one, two, result; // 数据输入

 @Override

 protected void onCreate(Bundle savedInstanceState) {

   superonCreate(savedInstanceState);

   setContentView(Rlayoutactivity_main);

   one = (EditText) thisfindViewById(Ridone);

   two = (EditText) thisfindViewById(Ridtwo);

   result = (EditText) thisfindViewById(Ridresult);

   button = (Button) thisfindViewById(Ridbutton);

   buttonsetOnClickListener(new ViewOnClickListener() {

     @Override

     public void onClick(View arg0) {

       // TODO Auto-generated method stub

       // 点击后获得用户录入的值

       int a = IntegerparseInt(onegetText()toString());

       int b = IntegerparseInt(twogetText()toString());

       // 创建意图

       Intent intent = new Intent(Mainthis, OtherActivityclass);

       // 将值传入意图

       intentputExtra("a", a);

       intentputExtra("b", b);

       startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果

     }

   });

 }

 // 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。

 @Override

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   // TODO Auto-generated method stub

   superonActivityResult(requestCode, resultCode, data);

   if (resultCode == 2) {// 如果第二个Activity(OtherActivity)正常结束(“2”为返回码resultCode)。

     if (requestCode == REQUESTCODE) {// 如果返回状态为1,即成功返回,就在意图的返回值中取出数据。

       int three = datagetIntExtra("three", 0);// 从第二个Activity中返回意图中的数据。

       resultsetText(StringvalueOf(three));

     }

   }

 }

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

   // Inflate the menu; this adds items to the action bar if it is present

   getMenuInflater()inflate(Rmenumain, menu);

   return true;

 }

}

9、在“OtherActivityjava”文件中,添加点击Button事件,使数据回传~,代码如下:package comandroidmyintent;

import androidappActivity;

import androidcontentIntent;

import androidosBundle;

import androidviewView;

import androidwidgetButton;

import androidwidgetEditText;

import androidwidgetTextView;

public class OtherActivity extends Activity {

 private Button button;

 private TextView textView;

 private EditText editText;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

   // TODO Auto-generated method stub

   superonCreate(savedInstanceState);

   setContentView(Rlayoutother);

   // 实例化button和textview

   button = (Button) thisfindViewById(Ridbutton2);

   textView = (TextView) thisfindViewById(Ridmsg);

   editText = (EditText) thisfindViewById(Ridthree);

   Intent intent = getIntent(); // 获取Intent

   // 取出Intent中的值

   int a = intentgetIntExtra("a", 0);

   int b = intentgetIntExtra("b", 0);

   textViewsetText(a + " + " + b + " = " + " ");

   // 添加点击事件并回传数据

   buttonsetOnClickListener(new ViewOnClickListener() {

     @Override

     public void onClick(View arg0) {

       // TODO Auto-generated method stub

       Intent intent = new Intent();// 重新声明一个意图。

       int three = IntegerparseInt(editTextgetText()toString());// 获取输入的值。

       intentputExtra("three", three); // 将three回传到意图中。

       // 通过Intent对象返回结果,调用setResult方法。

       setResult(2, intent);// resultCode为大于1的数,随意选取,为2即可。

       finish();// 结束当前Activity的生命周期。

     }

   });

 }

}

10、运行,结果:

(1)输入2和3:

(2)单击“计算结果”,跳转:

(3)输入5,单击“返回结果”,数据回传:

实现要点:

(1)在“Mainjava”中,创建Intent并启动Activity,调用“startActivityForResult”,并定义当前请求码;

(2)重写“onActivityResult”方法,并设置条件,若满足返回码值,则将第二个Activity中的数据传回来,赋给当前Activity的“result”编辑框;

(3)在“OtherActivityjava”中,再创建一个意图,将数据填写到意图中,通过意图将结果回传(通过“setResult”方法);

(4)结束当前Activity生命周期;

在目的activity中获取intent启动源的名字的方法

1 先说在setClass启动一个Activity的方法吧:

Intent intent = new Intent();

intentsetClass(this, CreatePlaylistclass) //参数一为当前Package的context,t当前Activity的context就是this,其他Package可能用到createPackageContex()参数二为你要打开的Activity的类名

startActivity(intent);

2 通过Component Name来打开的方式

Intent intent = new Intent();

intentsetAction(IntentACTION_MAIN); //添加一些特性,具体可以查看Intent文档,相关属性的介绍

intentaddCategory(IntentCATEGORY_LAUNCHER);

intentsetFlags(IntentFLAG_ACTIVITY_NEW_TASK | IntentFLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

//通过Bundle向要打开的的Activity传递一些数据

Bundle bundle = new Bundle();

bundleputString("data", new String(" Hello World"));

intentputExtras(bundle);

intentsetComponent(new ComponentName(

new String("comandroidtestActivity"), new String("comandroidtestActivitytestActivity")));

startActivity(intent);

speedsetOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<> adapter, View view,

int position, long id) {

sendBroadcast(new Intent("ACTION")putExtra("position", position));//通过广布把选中的位置发过去,如果你要发送这个位置的数据,那么你可以通过position获取。

}

在接收的类,写个广播接收器就可以了。

1拍照 (对于70以上的版本,不在允许直接访问uri)

`

若不指定输出路径intentputExtra(MediaStoreEXTRA_OUTPUT, getTempUri(srcActivity)); 在onActivityResult()中,通过

`

可以拿到uri,但获得的是被压缩过的。若指定intentputExtra(MediaStoreEXTRA_OUTPUT, uri);输出路径,则此处的intent为null,但可以使用我们存的uri读取照片,此时的照片没有被压缩。

2从相册中读取照片, 方法:

`

`

即使设置 intentputExtra(MediaStoreEXTRA_OUTPUT, getTempUri(srcActivity));输出路径,仍然不能从此路径中读取,只能在onActivityForResult()中通过eventuri = intentgetData();方式获得uri。

此种现象也好理解,拍照时产生新的,自然可根据设置的uri进行保存,而读取相册时,已经在目录中不能转移到自己设定的uri中。

Androidmanifestxml中

`

在 res/xml/provider_pathsxml

`

<xml version="10" encoding="utf-8">

<paths>

<external-path name="JDTobs" path=""/>

<files-path name="name" path="path" />

<cache-path name="name" path="path" /> <external-path name="name" path="path" />

<external-files-path name="name" path="path" />

<external-cache-path name="name" path="path" /> </paths> `

读取uri

我觉得这个写的挺好,分享一下

1创建一个DeviceAdminReceiver子类(DeviceAdminReceiver是广播接收者的子类)

import androidappadminDeviceAdminReceiver;

public class MyAdmin extends DeviceAdminReceiver {

}

2配置广播接收者清单文件

<xml version="10" encoding="utf-8">

<manifest xmlns:android=">

Android 中Activity之间的转跳是通过Intent来传递数据的,可以将URL放进Intent中,实现转跳后加载URL。

Android中intentputExtra(); 是用于Intent传递数据的。

Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。比如,有一个Activity希望打开网页浏览器查看某一网页的内容,那么这个Activity只需要发出WEB_SEARCH_ACTION给Android,Android就会根据Intent的请求内容,查询各组件注册时声明的IntentFilter,找到网页浏览器的Activity来浏览网页。 

Android的三个基本组件——Activity,Service和Broadcast Receiver——都是通过Intent机制激活的,不同类型的组件有不同的传递Intent方式:

要激活一个新的Activity,或者让一个现有的Activity做新的 *** 作,可以通过调用ContextstartActivity()或者ActivitystartActivityForResult()方法。 

要启动一个新的Service,或者向一个已有的Service传递新的指令,调用ContextstartService()方法或者调用ContextbindService()方法将调用此方法的上下文对象与Service绑定。

ContextsendBroadcast()、ContextsendOrderBroadcast()、ContextsendStickBroadcast()这三个方法可以发送Broadcast Intent。发送之后,所有已注册的并且拥有与之相匹配IntentFilter的BroadcastReceiver就会被激活。

Intent一旦发出,Android都会准确找到相匹配的一个或多个Activity,Service或者BroadcastReceiver作响应。所以,不同类型的Intent消息不会出现重叠,即Broadcast的Intent消息只会发送给BroadcastReceiver,而决不会发送给Activity或者Service。由startActivity()传递的消息也只会发给Activity,由startService()传递的Intent只会发送给Service。

需要在跳转的Intent对象中添加一个参数:

intentputExtra("comeFrom", activityName);

跳转到目标activity时带上当前activity的名字,这样才知道是从哪里跳转进来的。

以上就是关于android的页面跳转时,如A->B,这时候在B页面怎么获取A的类名全部的内容,包括:android的页面跳转时,如A->B,这时候在B页面怎么获取A的类名、如何从activity返回数据、如何在目的activity中获取intent启动源的名字等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9352153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存