如何传递 Intent Extras?
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");
}
};
以上是我的第二个活动的一部分。基本上,此代码显示手机 SD 卡中的视频缩略图。无论如何,当我单击缩略图时,我希望单击列表中的项目以打开下面发布的新活动,即 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;
}
}
});
}
}
}
正如您所看到的,我不知道如何正确传递意图附加内容,也不知道如何从第三个活动中获取它们。感谢所有帮助。谢谢。
====== Android Manifest ====== (这不是完整的清单文件)
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
您需要对传递给 startActivity()
的 Intent 实例调用 putExtra()
。在第二个Activity中,您可以调用getIntent()
(Activity的成员)来获取启动该Activity的Intent。在 onCreate()
中执行此操作。然后,它将返回一个 Intent 实例,您可以根据您打包的额外内容的类型来调用 get
。如果您的要求超出了支持的基本类型(已经实现 Parcelable 的事物和基本 java 类型),那么您将需要编写自己的类并实现 Parcelable 接口。
有关详细信息,请参阅 Intent 文档。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在
onCreate
方法的第二个活动中:and in second activity in
onCreate
method: