How could I Send Data(Text) to other apps(like mes

2019-06-08 05:34发布

How could I make a program that after entering a string and press a key, the string is stored in the phone (SharedPreferences), and then if I open the program to Send Sms(messages), 3 seconds later write in the Message EditText the string stored before.

so far I've done this:

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

  EditText stringinput = (EditText) findViewById(R.id.string);
  Button bu = (Button) findViewById(R.id.button1);
  stringinput.setImeOptions(EditorInfo.IME_ACTION_DONE); 
  bu.setOnClickListener(this);
}

@Override  
public void onClick(View v) {  
  EditText stringinput = (EditText) findViewById(R.id.string);
  String name = stringinput.getText().toString();   
  if(v.getId()==(R.id.string)){  
  SavePreferences("STRING",name);  
  }
}  

private void SavePreferences(String key, String value){  
  SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);  
  SharedPreferences.Editor editor = sharedPreferences.edit();  
  editor.putString(key, value);  
  editor.commit();  
 }  

private void LoadPreference(){         
      SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);           
      String strSavedMem1 = sharedPreferences.getString("STRING", ""); 
     /******************** stringoutput.append(strSavedMem1);*******************/
      }
}

Ok Now I do not Know how to move forward---> I think that the hardest work is in this function LoadPreferences(); , Perhaps I should Build an Implicit Intent

Intent SEND = new Intent(Intent.ACTION_SEND);

I don't Know , help me plese

1条回答
欢心
2楼-- · 2019-06-08 06:13

If you are trying to send messages to other applications running in android, or to leave messages for them, I would check out running services in seperate processes in the android service tutorial here: http://www.vogella.com/articles/AndroidServices/article.html You can have the service keep track of strings you pass it, and have other applications bind to the service to get them back.

Otherwise, if you just want to save strings to a file, a property file would do just fine. If other applications need to be able to read the file, save it to external storage. Theres a good tutorial for how this works on the main android site here: http://developer.android.com/guide/topics/data/data-storage.html

Edit: If you are dead set on using shared preferences to share strings between applications, another stack overflow user posted a tuturial on it here: How can I share a SharedPreferences file across two different android apps?

查看更多
登录 后发表回答