免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3386 | 回复: 1
打印 上一主题 下一主题

Android笔记——不同apk之间传递参数与数据 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-13 21:40 |只看该作者 |倒序浏览
Android笔记——不同apk之间传递参数与数据












android编程的时候,有时候需要在不同的apk之间传递参数或数据,下面是一个简单的例子:
APK(1)的程序代码:
  1. IntentSend.java:
  2. package com.is;

  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;

  8. public class IntentSend extends Activity {
  9. Channel channel = new Channel();
  10. Button szws;

  11.     /** Called when the activity is first created. */
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.         szws = (Button)findViewById(R.id.szws);
  17.         szws.setOnClickListener(new Button.OnClickListener(){
  18. public void onClick(View v) {
  19. Intent intent = new Intent();
  20. Bundle bundle = new Bundle();
  21. bundle.putString("channel", channel.channels[0]);
  22. intent.setClassName("com.bget", "com.bget.BinderGET");
  23. intent.putExtras(bundle);
  24. startActivity(intent);
  25. }
  26.         });
  27.     }
  28. }

  29. Channel.java:

  30. package com.is;

  31. public class Channel {
  32. String channels[] = {
  33.   "深圳卫视",
  34.   "深圳电视剧",
  35.   "深圳都市"
  36. };
  37. }
复制代码
APK(2)的程序代码:
  1. IntentGet.java:

  2. package com.ig;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;

  6. public class IntentGet extends Activity {
  7. TextView info;

  8.     /** Called when the activity is first created. */
  9.     @Override
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.main);  
  13.         info =(TextView)findViewById(R.id.info);
  14.        Bundle bundle = new Bundle();
  15.      bundle = this.getIntent().getExtras();
  16.         
  17.         info.setText("现在播放的是:" + bundle.getString("channel"));
  18.     }
  19. }
复制代码
程序中,主要是调用了Intent和Bundle的方法,Intent程序之间的跳转,Bundle程序之间数据的传递。

论坛徽章:
0
2 [报告]
发表于 2011-12-21 22:54 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP