Android从另一个应用程序发送字符串

时间:2014-09-08 10:32:14

标签: android service android-service intentfilter intentservice

我正在开发两个应用程序,ApplicationA和ApplicationB,ApplicationA将String发送到ApplicationB,我在ApplicationB Activity上显示接收字符串。现在,当我点击ApplicationA上的按钮并希望发送一个按钮时,它正常工作。字符串到ApplicationB,出现弹出窗口,我从这个弹出窗口中选择了ApplicationB,我想当我从ApplicationA点击Button时弹出窗口没有出现,直接我的ApplicationB打开并显示收到的字符串,我也想执行这个任务在后台服务中,我怎么能实现这个目标?

我的应用程序主要活动:

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

这是我的ApplicationA Screen Shot:

enter image description here

当点击按钮时,会出现弹出窗口,我从这个弹出窗口中选择了ApplicationB:

enter image description here

这是我的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屏幕截图:

enter image description here

注意: -

现在我想当我点击ApplicationA上的按钮时,完成过程将在后台服务中执行,当后台服务完成后,我的ApplicationB活动就会打开,接收字符串,我如何通过服务实现这一点,当点击按钮时,弹出窗口不会显示给用户,请有人帮助我,非常感谢Alot提前

2 个答案:

答案 0 :(得分:0)

在启动其他应用程序的意图中,您必须提及其包名称,如:

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

答案 1 :(得分:0)

我已经解决了我的问题:

来自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();
}
}