我正在使用 Android。我在活动 A 中有一个按钮和 EditText。当我点击按钮时,它会带着一个 Intent 转到活动 B。我在活动 B 中有一个 ListView。我单击列表项并将值存储在字符串中,然后再次返回带有意图的活动 A。现在我想在活动 A 的 EditText 字段中设置值。这可能吗????
我是 Android 编程的新手 - 所以我对“上下文”和“意图”不是很清楚。
我想知道有没有办法从 Service 类访问Activity?即假设我有 2 个类 - 一个从“活动”扩展,另一个从“服务”扩展,我在我的活动类中创建了一个意图来启动服务。
或者,如何从我的“活动”类访问“服务”类实例 - 因为在这样的工作流中,服务类不是由我的活动代码直接实例化的。
public class MainActivity extends Activity {
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, CommunicationService.class));
.
.
}
public class CommunicationService extends Service implements ..... {
.
.
@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
super.onStartCommand(intent, flags, startId);
....
}
}
Run Code Online (Sandbox Code Playgroud) 我可以使用onBackpressed以下方法从意图中获得结果:
returnIntent.putExtra("isBack", true);
setResult(RESULT_OK,returnIntent);
finish();
Run Code Online (Sandbox Code Playgroud)
这是我的onActivityResult:
if (requestCode == 1)
{
if(resultCode == RESULT_OK){
boolean isBack = data.getBooleanExtra("isBack", false);
if(isBack == true)
searchEditText.setText("");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,现在我需要做setResult not in the onBackpressed,所以我在想onDestroy.
我试图移动setResult到onDestroy但onActivityResult不能得到我的结果。
我也尝试过onStop和onPause,但它们都不起作用。
请帮助我,谢谢你的帮助
大家好,如果你愿意帮助我。我被困在如何从另一个活动中检索包数据。基本上我有两个活动,当我在第一个活动上按下按钮时,它将继续第二个活动,然后设置稍后要在第一个活动上传递的字符串值。我所做的是使用捆绑包来放置字符串值。我的问题是如何从第二个活动中获取(字符串的)包值并将其返回到第一个活动?这是我的代码:
FirstActivity(继续第二个活动):
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(this,
SecondActivity.class), REQUEST_CODE_SAMPLE);
}
});
Run Code Online (Sandbox Code Playgroud)
SecondActivity:(返回捆绑包值)
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
bundle.putString("A", "Aloha");
bundle.putString("B", "Bubbles");
setResult(
Activity.RESULT_OK,
getIntent().putExtras(b));
}
});
}
Run Code Online (Sandbox Code Playgroud)
FirstActivity(将检索包值):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SAMPLE:
if (resultCode == RESULT_OK) {
bundle = getIntent().getExtras();
//WHAT TO DO TO GET THE BUNDLE VALUES//
String a = …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过发送以下意图对 Deezer 进行搜索:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.deezer.com/search/" + query));
this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在以前的版本中,Web 浏览器打开时带有一个链接,上面写着“在 Deezer 中打开”(或类似内容)。但在当前版本中,这不再起作用。
有没有办法打开 Deezer 应用程序并执行搜索?我已经尝试了以下(没有成功):
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("deezer://search/" + query));
this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud) 我对 android 编程(以及一般的 java)很陌生,并且遇到了一个让我难倒的问题。我正在构建一个基本的应用程序(用于练习),它可以访问一个已经建立的网站 www.shodan.io 。据我所知,这个特定网站还没有安卓应用程序,我非常喜欢它的研究。所以我有一个有 4 个活动的应用程序,所有这些活动都加载得很漂亮(在堆栈论坛的很多帮助之后)。它们是:LoadingPage、MainActivity、SearchPage 和 ExploitSearch。在我的 MainActivity 上,我有一个 webview 来显示不同的页面、切换按钮、徽标等。我的搜索和利用按钮都可以毫无问题地加载相应的页面。现在我的问题/问题。
我想让搜索和利用活动接受输入(这是他们目前的唯一目的),将所述输入保存到一个字符串中,然后可以从我的 MainActivty 访问该字符串并用作我的 url 的搜索查询。
到目前为止,我的搜索已经出现了许多论坛,它们告诉我们如何从活动 1 中获取数据,并使用“意图”让活动 2 可以读取它。但是,我似乎找不到任何资源来做相反的事情,或将其保存为字符串(可能在临时文件中,最好是以后可以回忆的东西......有点像“输入”+\n 所以它不是覆盖)
谢谢你的时间(ps。让我知道需要的任何代码部分,因为我不想用 4 个活动和它们各自的布局文件填充这个线程。)
I have the following code which used to send text from my app to Email:
Intent mail = new Intent(Intent.ACTION_VIEW);
mail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { });
mail.setData(Uri.parse(""));
mail.putExtra(Intent.EXTRA_SUBJECT, "Country Decryption");
mail.setType("plain/text");
mail.putExtra(Intent.EXTRA_TEXT, "my text");
ctx.startActivity(mail);
Run Code Online (Sandbox Code Playgroud)
It works , but as you see, it uses Gmail app, how do I make it use Email application instead of Gmail?
and what about sharing to Facebook ? I found that Facebook does not support sharing using intent anymore, and …
我有一个奇怪的错误。
我收到了一个带有 3 个按钮(后退、下一步、播放/暂停)的通知。我想在启动服务的这三个按钮中的每一个上设置不同的 pendingIntent。这是我的代码:
Intent playPauseIntent = new Intent(MyApp.mainActivity, NotificationService.class);
playPauseIntent.putExtra("do_action", "play");
contentView.setOnClickPendingIntent(R.id.notifPlayPauseImageView, PendingIntent.getService(MyApp.mainActivity, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Intent nextRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
nextRadioIntent.putExtra("do_action", "next");
contentView.setOnClickPendingIntent(R.id.notifNextImageView, PendingIntent.getService(MyApp.mainActivity, 0, nextRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Intent previousRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
previousRadioIntent.putExtra("do_action", "previous");
contentView.setOnClickPendingIntent(R.id.notifPreviousImageView, PendingIntent.getService(MyApp.mainActivity, 0, previousRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notification.contentView = contentView;
mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)
然后,在我的服务中,我将“do_action”的意图和值定义为额外的:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null && intent.getExtras() != null)
{
String action = (String) intent.getExtras().get("do_action"); // BUG HERE ! I always get …Run Code Online (Sandbox Code Playgroud) java android android-intent android-notifications android-pendingintent
我能够启动谷歌地图意图
Uri location = Uri.parse("geo:0,0");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
startActivity(mapIntent);
Run Code Online (Sandbox Code Playgroud)
如何在驾驶模式下启动意图(没有目的地,除非之前在地图中设置),使其看起来像下面的屏幕截图?
我尝试设置,mode=driving但只是打开了选择“驾驶”选项卡的常规地图:
Uri location = Uri.parse("geo:0,0?mode=driving");
Run Code Online (Sandbox Code Playgroud) 我正在我的应用程序的 AndroidManifest.xml 文件中为电池电源连接和断开连接事件添加意图过滤器,并按照此链接中的说明注册 PowerConnection 接收器:https : //developer.android.com/training/monitoring-device-state/battery-monitoring html的。但是当我用电源连接和断开我的设备时,接收器没有被调用。当我在我的活动类中以编程方式注册同一个接收器时,它工作正常。当我的应用程序未运行时,我想通过在 AndroidManifest.xml 中手动注册它来使用电源事件调用此接收器。请帮我解决这个问题。
android ×10
android-intent ×10
java ×2
deezer ×1
email ×1
google-maps ×1
input ×1
share ×1
uri ×1