我在MainActivity中有两个按钮,这些按钮具有OnLongClickListener,并且它们都启动另一个名为Pop.class的活动。 所以一切正常。 我想要的是发送与下一个活动中的按钮相对应的url。
因此,当用户按下MainActivity活动中的按钮时,他会将该应用程序带到Pop活动中,当点击Pop活动中的按钮时,他将被重定向到YouTube应用程序中对应于已由前一个按钮(此处为b26或b27)“声明”的url。
问题是我不知道如何让我的字符串成为Uri.parse()方法的真实URL。 此外,我使用了一个字符串,因为我想在每个按钮中放入不同的url。
为此,我使用了putExtra和getExtra,但我不明白为什么字符串url不起作用,所以我没有修改b26代码。 :(
这就是为什么我问你一些帮助
这是MainActivity活动:
Button b26 = (Button) findViewById(R.id.b26); b26.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (SoundBuildingOnFiire.isPlaying()) { SoundBuildingOnFiire.seekTo(0); } else {SoundBuildingOnFiire.start();}}}); b26.setOnLongClickListener( new Button.OnLongClickListener() { public boolean onLongClick(View v) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); startActivity(new Intent(MainActivity.this, Pop.class)); return true;}}); Button b27 = (Button) findViewById(R.id.b27); b27.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (SoundJackson.isPlaying()) { SoundJackson.seekTo(0); } else {SoundJackson.start();}}}); b27.setOnLongClickListener( new Button.OnLongClickListener() { public boolean onLongClick(View v) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); startActivity(new Intent(MainActivity.this, Pop.class)); getIntent().putExtra("SoundName", "https://www.youtube.com/"); return true;}}); }这是Pop活动:
public class Pop extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popup); final String SoundName = getIntent().getStringExtra("SoundName"); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; getWindow().setLayout((int) (width * .8), (int) (height * .4)); TextView txtLabel = (TextView)findViewById(R.id.textView); txtLabel.setText("En savoir plus sur ce Son"); Button button = (Button)findViewById(R.id.youtube); button.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(SoundName)); startActivity(viewIntent); } }); }}
谢谢你的时间
I have two buttons in my MainActivity, these buttons have OnLongClickListener, and they both start another activity called Pop.class. So everything works fine. What I want is to send a url that correspond to the button in the next activity.
So like this when the user press the button in the MainActivity activity, he is bring by the application on the Pop activity and when clicking on the button in the Pop activity he will be redirected to a YouTube video on the YouTube app that correspond to the url that has been "declared" by the previous button (here b26 or b27).
The problem is that I don't know how to make my string a true URL for the Uri.parse() method. Also I've used a string because I wanted to put different url in each button.
For this I have used putExtra and getExtra but I don't understand why string url is not working whit the b27 button so I didn't modify the b26 code. :(
This is why I'm asking you some help
This is the MainActivity activity :
Button b26 = (Button) findViewById(R.id.b26); b26.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (SoundBuildingOnFiire.isPlaying()) { SoundBuildingOnFiire.seekTo(0); } else {SoundBuildingOnFiire.start();}}}); b26.setOnLongClickListener( new Button.OnLongClickListener() { public boolean onLongClick(View v) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); startActivity(new Intent(MainActivity.this, Pop.class)); return true;}}); Button b27 = (Button) findViewById(R.id.b27); b27.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { if (SoundJackson.isPlaying()) { SoundJackson.seekTo(0); } else {SoundJackson.start();}}}); b27.setOnLongClickListener( new Button.OnLongClickListener() { public boolean onLongClick(View v) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); startActivity(new Intent(MainActivity.this, Pop.class)); getIntent().putExtra("SoundName", "https://www.youtube.com/"); return true;}}); }This is the Pop activity :
public class Pop extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popup); final String SoundName = getIntent().getStringExtra("SoundName"); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; getWindow().setLayout((int) (width * .8), (int) (height * .4)); TextView txtLabel = (TextView)findViewById(R.id.textView); txtLabel.setText("En savoir plus sur ce Son"); Button button = (Button)findViewById(R.id.youtube); button.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(SoundName)); startActivity(viewIntent); } }); }}
Thanks for your time
最满意答案
这是因为你没有将URL传递给POP活动,并且每次都是空的。
更改
startActivity(new Intent(MainActivity.this, Pop.class)); getIntent().putExtra("SoundName", "https://www.youtube.com/");至
Intent intent = new Intent(MainActivity.this, Pop.class); intent.putExtra("SoundName", "https://www.youtube.com/"); startActivity(intent);It happens because of you doesn't pass URL to POP activity, and it is null everytime.
change
startActivity(new Intent(MainActivity.this, Pop.class)); getIntent().putExtra("SoundName", "https://www.youtube.com/");to
Intent intent = new Intent(MainActivity.this, Pop.class); intent.putExtra("SoundName", "https://www.youtube.com/"); startActivity(intent);更多推荐
发布评论