Android中intent的使用

程序员的店http://paperman.taobao.com/

插个广告,小店刚开张,欢迎各位同道中人的亲光顾,照顾生意哈,我们要做技术上滴大牛,还要穿的有范儿!酷

 

一、intent 
 发送intent的Activity
 Intent intent = new Intent();
 Bundle bundle = new Bundle();
 bundle.putString("key_what",key);
 intent.setClass(Activity.this,intentActivity.class);
 intent.putExtras(bundle);
 startActivity(intent);
 

 接收intent的Activity
 Intent intent = getIntent();
 Bundle bunde = intent.getExtras();
 String string = bunde.getString("key_what");
  
  
二、在Activity中打开一个Activity:
 Intent intent = new Intent();
 intent.setClass(ClassOne.this,ClassTwo.class);
 startActivity(intent);

 

不在Activity中,打开一个Activity,如sevice中

 Intent intent = new Intent(context, ClassTwo.class);
 intent.setClass(context, ClassTwo.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(intent);

 

NOTE:要添加Flags



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android StudioIntent是一种用于在不同组件之间传递数据的机制。Intent可以用于启动活动、服务和广播接收器,以及在这些组件之间传递数据。 要使用Intent,您需要创建一个Intent对象,并指定要启动的组件和要传递的数据。例如,要启动一个活动并传递一些数据,您可以使用以下代码: ``` Intent intent = new Intent(this, MyActivity.class); intent.putExtra("key", "value"); startActivity(intent); ``` 在这个例子,我们创建了一个Intent对象,并指定要启动的活动是MyActivity。我们还使用putExtra()方法将一个键值对添加到Intent,以便在启动的活动使用。 要在启动的活动获取传递的数据,您可以使用以下代码: ``` Intent intent = getIntent(); String value = intent.getStringExtra("key"); ``` 在这个例子,我们使用getIntent()方法获取传递给活动的Intent对象。然后,我们使用getStringExtra()方法获取传递的字符串数据,并指定键名为“key”。 除了传递数据,Intent还可以用于启动其他组件,例如服务和广播接收器。要启动服务,您可以使用以下代码: ``` Intent intent = new Intent(this, MyService.class); startService(intent); ``` 在这个例子,我们创建了一个Intent对象,并指定要启动的服务是MyService。然后,我们使用startService()方法启动服务。 要注册广播接收器并接收广播消息,您可以使用以下代码: ``` IntentFilter filter = new IntentFilter(); filter.addAction("com.example.MY_ACTION"); MyBroadcastReceiver receiver = new MyBroadcastReceiver(); registerReceiver(receiver, filter); ``` 在这个例子,我们创建了一个IntentFilter对象,并指定要接收的广播消息是“com.example.MY_ACTION”。然后,我们创建了一个广播接收器对象,并使用registerReceiver()方法注册它。 以上是Android Studio使用Intent的基本方法。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值