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

ぃ、小莉子 提交于 2019-12-23 03:28:10

问题


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:


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?



来源:https://stackoverflow.com/questions/16000240/how-could-i-send-datatext-to-other-appslike-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!