从SD卡读取文本文件
我有一个应用程序可以发送短信来检查我的数据包中剩余的MB。我有一个带有按钮和文本视图的布局。当我按下按钮时,我会向电话运营商发送一条消息。然后我有一个广播接收器,它监听传入的消息,并将消息正文保存到文本文件中。当我从操作员那里得到答案时,我想在我的文本视图中显示此文本。
这是我的代码:
public class bonbon3 extends Activity
{
Button btnStanje;
Context context=this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
btnStanje = (Button) findViewById(R.id.provjeriStanje);
btnStanje.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = "0977";
String message = "stanje";
sendSMS(phoneNo, message);
Toast.makeText(getBaseContext(), "Zahtjev za provjeru stanja paketa je poslan, odgovor očekuj uskoro!", Toast.LENGTH_SHORT).show();
File root = Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/Bonbon info");
dir.mkdirs();
File f = new File(dir, "test.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
}
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(text);
}
});
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
现在,我试图在收到短信回复之前从文件中读取这段代码,所以我知道这是错误的,但我不知道在收到短信回复后如何将文本加载到textView?
I have an app that send SMS for checking remaining MB in my data package. I have a layout with a button and a text view. When I press my button, I send a message to my phone operator. Then I have a broadcast receiver, that listens to incoming messages, and saves message body to a text file. I Want to show this text in my text view when I get answer from my operator.
This is my code:
public class bonbon3 extends Activity
{
Button btnStanje;
Context context=this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
btnStanje = (Button) findViewById(R.id.provjeriStanje);
btnStanje.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = "0977";
String message = "stanje";
sendSMS(phoneNo, message);
Toast.makeText(getBaseContext(), "Zahtjev za provjeru stanja paketa je poslan, odgovor očekuj uskoro!", Toast.LENGTH_SHORT).show();
File root = Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/Bonbon info");
dir.mkdirs();
File f = new File(dir, "test.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
}
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(text);
}
});
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
Now, this code I am trying to read from file, before answer SMS is received, so I know this is wrong, but I don't know how to load text to textView after I get SMS answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗨,我认为 此链接将对你很有帮助
在android中从sdcard读取文本文件与在java中读取文本文件相同..
hi i think this link will be very helpful for you
Reading a text file from sdcard in android is as same as reading a text file in java..
Goran,我无法在线回复,但如果您点击我的链接,几乎有您需要的一切。
我几乎完全按照这个例子进行操作,效果很好。
您只需要在活动中实现消息处理程序(而不是示例中的服务)并从服务推送消息(而不是在此处接收消息),但除此之外,这是完全相同的。
因此,在您的活动中,您应该有这样的内容:
在您的服务(接收短信的位)中,您应该有类似的内容:
Goran, I cannot reply inline, but if you follow my link, there is pretty much everything you need.
I followed this example almost excatly and it worked out well.
You just need to implement a message handler in your activity (instead of the service as in the example) and push the message from the service (instead of recieving it here) but except that, this is exactely the same.
So in your activity, you should have something like this :
In you service (the bit that recieves the SMS) you should have something like:
我认为您宁愿在您的活动和广播接收器之间建立消息通信(这是一项服务,不是吗?)。
你一定会有一个循环,它会尝试连续读取文件,直到它出现在那里,而 ci 感觉不正确。
不要将 SMS 存储在文本文件中,使用消息传递系统将内容直接从活动发送到服务。
对于活动和服务之间的消息传递,请查看 Android 开发人员指南中有关绑定服务的信息,尤其是 关于Messenger的部分(您的服务不需要绑定)。
I think you would rather establish a messaging communication between your activity and your broadcast reciever (that is a service isn't it?).
You are bound to have a loop that will try to read the file continously until it is there whci does not feel right.
Do not store the SMS in a text file send the content directly from the activity to the service using the messaging system.
For messaging between activities and services, have a look in the Android developper guide about Bound services and especially the section about messenger (your service does not need to be bound).