Android的另一个应用程序发送的字符串 [英] Android Sending String from another app

查看:140
本文介绍了Android的另一个应用程序发送的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在两个应用程序,ApplicationA和ApplicationB,ApplicationA发送字符串ApplicationB,我显示在ApplicationB Activity.now全部的寄托接收字符串工作正常,当我点击从ApplicationA一个按钮,想送字符串ApplicationB,有弹出出现,我从这个弹出选择ApplicationB,我想,当我从ApplicationA按钮弹出不会出现,直接我ApplicationB开放点击并显示recieving串,我也想执行此任务在后台服务,我怎么能做到这一点?

I am working on two app, ApplicationA and ApplicationB, ApplicationA send String to ApplicationB,and i am display the receiving string on ApplicationB Activity.now everthing is working fine,when i Click on a Button from ApplicationA and want to send a string to ApplicationB,there is popup appear and i am select the ApplicationB from this popup,i want when i click on Button from ApplicationA the popup does not appear and directly my ApplicationB open and display the recieving string,also i want to perform this task in a background services,how i can achieve this?

我ApplicationA MAinActivity:

My 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;


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屏幕截图:

This is my ApplicationA Screen Shot:

当点击按钮,弹出就会出现,我从这个弹出选择ApplicationB:

When click on button the popup will appear and i am selecting ApplicationB from this popup:

这是我的ApplicationB MainActivty:

This is my 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文件:

and This is my ApplicationB menifast file:

<?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屏幕截图:

and this the ApplicationB screen shot when i am Receive the the string from ApplicationA:

注意: -

现在我想,当我点击来自ApplicationA按钮,整个过程将在后台服务来执行,而且当后台服务做了,那么我的ApplicationB活动是与接收串开路,我怎样才能通过服务来实现这一点,当点击按钮,在弹出不会出现用户,请有人帮助我,非常感谢提前

now i want to when i click on button from ApplicationA,complete process will be perform in background services,and when background services is done,then my ApplicationB activity is open with the receiving string,how i can achieve this through services,when click on button the popup will not appear to the user,please some one help me out,Thanks Alot in advance

推荐答案

我已经解决了我的问题:

I have solved my Problem:

我MainActivity从ApplicationA:

my MainActivity from ApplicationA:

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

这篇关于Android的另一个应用程序发送的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆