在活动之间发送URL(Sending URLs between Activities)

编程入门 行业动态 更新时间:2024-10-17 15:30:29
在活动之间发送URL(Sending URLs between Activities)

我在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);

更多推荐

本文发布于:2023-08-02 09:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sending   URL   Activities   URLs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!