【问题标题】:Android Sending String from another appAndroid 从另一个应用程序发送字符串
【发布时间】: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


【解决方案1】:

我已经解决了我的问题:

我的来自 ApplicationA 的 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;
import android.widget.EditText;
import android.widget.Toast;


 public class MainActivity extends Activity {

Button sendstring;
EditText edit_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//    startService(new Intent(MainActivity.this, MyService.class));

    edit_text = (EditText) findViewById(R.id.edit_text);

    sendstring = (Button) findViewById(R.id.sendstring);

    sendstring.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            startService(new Intent(MainActivity.this, MyService.class));


        }
    });
}


@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);
}
}

这是我的后台服务类:

package com.example.applicationa;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
 import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;

String et_name;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
//  Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

//  et_name = getIntent().getStringExtra("dealer_id");
    /*
    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)));*/

    /*player = MediaPlayer.create(this, R.raw.braincandy);
    player.setLooping(false);*/ // Set looping
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
     if(intent != null){
         et_name = intent.getStringExtra("et_name");
           }
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
//  player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "" + et_name, Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

    Intent sendIntent = new Intent();
    sendIntent.setClassName("com.example.applicationb",
            "com.example.applicationb.MainActivity");
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hi Farhan Shah,Welcome to AppB");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);

//  player.start();
}
}

【讨论】:

  • 是的,就是这样做的。但是……这样好吗?这是一项服务......用户正在做某事,突然出现另一个应用程序。不是很好。只需一个通知就可以做得更好。
  • 我怎样才能使它成为面糊?
  • 兄弟,我不想要通知、提示等,我只想要当我点击按钮时服务启动,另一个应用程序显示字符串,我已经通过后台服务成功完成了。..跨度>
【解决方案2】:

为了启动您的其他应用,您必须提及其包名称,例如:

            intent.setClassName("com.farhan.appb",
                    "com.farhan.appb.MainActivity");

【讨论】:

  • 例如,当我单击 applicationA 上的按钮时,服务启动并执行后台操作并在 ApplicationB 中接收字符串。
  • 无论您是否使用服务或其他什么来启动它都无关紧要。如果您希望用户不受 android 从应用列表中进行选择的困扰,那么您必须像我向您展示的那样修改您的意图。
  • 但它不起作用,我如何在后台服务中执行此任务?
  • 这是两个问题。让我们从第一个开始。为什么它不起作用?请显示您在 onClick() 中尝试过的代码。
  • 兄弟我已经解决了我的问题,谢谢你的支持:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 2015-01-06
相关资源
最近更新 更多