如何通过附加意图? [英] How to pass Intent Extras?

查看:148
本文介绍了如何通过附加意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类菜单扩展活动{
//设置常量MediaStore进行查询,并显示视频
私人最终静态乌里MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
私人最终静态字符串_ID = MediaStore.Video.Media._ID;
私人最终静态字符串MEDIA_DATA = MediaStore.Video.Media.DATA;
//标志,其中一个用于图像的选择
私人的GridView _gallery;
私人光标_cursor;
私人诠释_columnIndex;
私人INT [] _​​videosId;
私人乌里_contentUri;


受保护的语境_context;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    _context = getApplicationContext();
    _gallery =(GridView控件)findViewById(R.id.videoGrdVw);
    //设置默认为外部/ SD卡的URI
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    //初始化视频URI
    // showToast(_contentUri.getPath());
    initVideosId();
    //设置库适配器
    setGalleryAdapter();
}
私人无效setGalleryAdapter(){
    _gallery.setAdapter(新VideoGalleryAdapter(_context));
    _gallery.setOnItemClickListener(_itemClickLis);

}
私人AdapterView.OnItemClickListener _itemClickLis =新OnItemClickListener()
{
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长ID)
    {
        //现在我们要实际获得的文件的数据位置
        的String []凸出= {MEDIA_DATA};
        //我们再次要求我们的光标
        _cursor = managedQuery(_contentUri,
                PROJ,//哪些列返回
                空,// WHERE子句;返回哪些行(所有行)
                空,// WHERE子句选择论(无)
                空值); //订单by子句(按名称升序)
        //我们想要得到的数据URI的列索引
        诠释计数= _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        //让移动到所选择的项目光标
        _cursor.moveToPosition(位置);

        意图I =新意图();
        i.putExtra(MNT / SD卡-EXT,_ID);
        startActivity(com.ave.EDITOR);


    }
};
 

以上是我的第二个活动的一部分。基本上,这code显示从手机的SD卡的视频缩略图。不管怎么说,当我点击缩略图我想在列表中的项目点击打开一个新的活动贴在下面,这是一个ViewView。

 公共类编辑器扩展了活动{

的ImageButton VIDEO1;
INT isClicked = 0;
的ImageButton音频;
INT isClicked1 = 0;
私人字符串路径=MNT / SD卡-EXT;
私人VideoView mVideoView;

    @覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
    的setContentView(R.layout.editor);
    mVideoView =(VideoView)findViewById(R.id.videoView);

    int数据= getIntent()getExtras(MNT / SD卡-EXT).getInt(com.ave.EDITOR)。

    如果(路径==MNT / SD卡-EXT){
        //告诉用户提供媒体文件的URL /路径。
        Toast.makeText(
                Editor.this,
                请编辑VideoViewDemo活动,并设置路径
                        +变到你的媒体文件的网址/路径,
                Toast.LENGTH_LONG).show();

    } 其他 {

        / *
         *另外,流媒体可以使用
         * mVideoView.setVideoURI(Uri.parse(URLstring));
         * /
        mVideoView.setVideoPath(路径);
        mVideoView.setMediaController(新的MediaController(本));
        mVideoView.requestFocus();



    VIDEO1 =(的ImageButton)findViewById(R.id.video1);
    video1.setOnClickListener(新OnClickListener(){

        公共无效的onClick(视图v){
            如果(isClicked == 0){
                video1.setImageResource(R.drawable.video_ pressed);
                isClicked = 1;
            } 其他 {
                video1.setImageResource(R.drawable.video1);
                isClicked = 0;
            }
          }
     });

    音频=(的ImageButton)findViewById(R.id.audio);
    audio.setOnClickListener(新OnClickListener(){

        公共无效的onClick(视图v){
            如果(isClicked1 == 0){
                audio.setImageResource(R.drawable.audio_ pressed);
                isClicked1 = 1;
            } 其他 {
                audio.setImageResource(R.drawable.audio);
                isClicked1 = 0;
            }
          }
      });
    }
  }
}
 

正如你所看到的,我不知道如何正确地传递的意图演员,我也不知道如何从第三活动得到他们。所有帮助AP preciated。谢谢。

====== Android清单======(这不是完整的清单文件)

 <活动机器人:菜单名称=机器人:标签=@字符串/ APP_NAME机器人:screenOrientation =景观与GT;
      <意向滤光器>
            <作用机器人:名称=com.ave.CLEARSCREEN/>
            <类机器人:名称=android.intent.category.DEFAULT/>
      &所述; /意图滤光器>
< /活性GT;
<活动机器人:名称=机器人编辑:screenOrientation =景观与GT;
      <意向滤光器>
            <作用机器人:名称=com.ave.EDITOR/>
            <类机器人:名称=android.intent.category.DEFAULT/>
      &所述; /意图滤光器>
< /活性GT;
< /用途>
< /舱单>
 

解决方案

 意向书我=新的意向书(com.ave.EDITOR);
i.putExtra(MNT / SD卡-EXT,_ID);
startActivity(ⅰ);
 

和在的onCreate 第二项活动方式:

 字符串数据= getIntent()getStringExtra(MNT / SD卡-EXT)。
 

     public class Menus extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
//flag for which one is used for images selection
private GridView _gallery; 
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;


protected Context _context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _context = getApplicationContext();
    _gallery = (GridView) findViewById(R.id.videoGrdVw);
    //set default as external/sdcard uri
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    //initialize the videos uri 
    //showToast(_contentUri.getPath());
    initVideosId();
    //set gallery adapter
    setGalleryAdapter();
}
private void setGalleryAdapter() {
    _gallery.setAdapter(new VideoGalleryAdapter(_context));
    _gallery.setOnItemClickListener(_itemClickLis);

}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        int count = _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        _cursor.moveToPosition(position);

        Intent i = new Intent();
        i.putExtra("mnt/sdcard-ext", _ID);
        startActivity("com.ave.EDITOR");


    }
};

The Above is part of my Second Activity. Basically This code Displays Video Thumbnails from the SD card of the phone. Anyways, When i click on a thumbnail i want the Item in the list clicked to open to a new activity posted below, which is a ViewView.

public class Editor extends Activity {

ImageButton video1;
int isClicked = 0;
ImageButton audio;
int isClicked1 = 0;
private String path = "mnt/sdcard-ext";
private VideoView mVideoView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.editor);
    mVideoView = (VideoView) findViewById(R.id.videoView);

    int data = getIntent().getExtras("mnt/sdcard-ext") .getInt("com.ave.EDITOR");

    if (path == "mnt/sdcard-ext") {
        // Tell the user to provide a media file URL/path.
        Toast.makeText(
                Editor.this,
                "Please edit VideoViewDemo Activity, and set path"
                        + " variable to your media file URL/path",
                Toast.LENGTH_LONG).show();

    } else {

        /*
         * Alternatively,for streaming media you can use
         * mVideoView.setVideoURI(Uri.parse(URLstring));
         */
        mVideoView.setVideoPath(path);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();



    video1 = (ImageButton) findViewById(R.id.video1);
    video1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked == 0) {
                video1.setImageResource(R.drawable.video_pressed);
                isClicked = 1;
            } else {
                video1.setImageResource(R.drawable.video1);
                isClicked = 0;
            }
          }
     });

    audio = (ImageButton) findViewById(R.id.audio);
    audio.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked1 == 0) {
                audio.setImageResource(R.drawable.audio_pressed);
                isClicked1 = 1;
            } else {
                audio.setImageResource(R.drawable.audio);
                isClicked1 = 0;
            }
          }
      });
    }
  }
}

As you can see I don't know how to properly pass the intent extras, nor do i know how to get them from the 3rd activity. All help is appreciated. Thank you.

====== Android Manifest ====== (This is not the full manifest file)

<activity android:name=".Menus" android:label="@string/app_name" android:screenOrientation="landscape" >
      <intent-filter>
            <action android:name="com.ave.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
</activity>
<activity android:name=".Editor" android:screenOrientation="landscape" >
      <intent-filter>
            <action android:name="com.ave.EDITOR" />
            <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
</activity>
</application>
</manifest>

解决方案

Intent i = new Intent("com.ave.EDITOR");
i.putExtra("mnt/sdcard-ext", _ID);
startActivity(i);

and in second activity in onCreate method:

String data = getIntent().getStringExtra("mnt/sdcard-ext");

这篇关于如何通过附加意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆