【发布时间】:2014-11-01 13:17:48
【问题描述】:
我正在开发两个应用程序,ApplicationA 和 ApplicationB,ApplicationA 向 ApplicationB 发送字符串,我在 ApplicationB Activity 上显示接收字符串。现在一切正常,当我单击 ApplicationA 中的按钮并想要发送到ApplicationB的字符串,出现弹出窗口,我从这个弹出窗口中选择ApplicationB,我想当我从ApplicationA单击按钮时弹出窗口不出现,直接我的ApplicationB打开并显示接收字符串,我也想执行这个任务在后台服务中,我该如何实现?
我的应用程序A MAinActivity:
package com.example.applicationa;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button sendstring;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendstring = (Button) findViewById(R.id.sendstring);
sendstring.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hi Farhan Shah,Welcome to AppB");
sendIntent.setType("text/plain");
// startActivity(sendIntent);
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的应用程序屏幕截图:
当点击按钮时,弹出窗口会出现,我从这个弹出窗口中选择 ApplicationB:
这是我的 ApplicationB MainActivty:
package com.example.applicationb;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text_recieve;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_recieve = (TextView) findViewById(R.id.text_recieve);
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
text_recieve.setText(sharedText);
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的 ApplicationB menifast 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.applicationb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我从 ApplicationA 接收字符串时的 ApplicationB 屏幕截图:
注意:-
现在我想当我从 ApplicationA 中单击按钮时,将在后台服务中执行完整的过程,当后台服务完成后,我的 ApplicationB 活动将使用接收字符串打开,我如何通过服务来实现这一点,当点击按钮时,弹出窗口不会出现在用户面前,请有人帮助我,非常感谢提前
【问题讨论】:
-
请缩短您的问题!
-
@ZerO 我发了一个详细的问题,供击球手理解,简单易懂,欢迎复习:)
-
是的,但在我看来,这太详细了。我不会读它,因为我没有时间阅读所有这些。
-
@ZerO 问题的关键是,我正在从 ApplicationA 向 ApplicationB 发送字符串,当我点击 AplliationA 的按钮时,会出现弹出窗口,我正在从这个弹出窗口中选择 ApplicationB,然后我成功从 ApplicationB 接收字符串,现在我只想当我单击 applicationA 中的按钮时后台服务运行,并且用户侧没有弹出窗口,当该过程完成时,ApplicationB 使用接收字符串打开,这是 waht被通缉..
-
@ZerO 希望现在你明白我的意思了吗??
标签: android service android-service intentfilter intentservice