RegisterReceiver里面IntentService,跟踪SmsManager [英] RegisterReceiver inside IntentService , Tracking SmsManager
问题描述
我想通过短信使用扩展'IntentService课堂上发后的进展进行跟踪。
I am trying to track after sms sending progress by using class that extends 'IntentService'.
发送消息,我看到它在其他设备中,问题是'广播接收器
不会运行的onReceive方法。
The message is sent and i see it on other device , the problem is that the 'BroadcastReceiver' wont run the onReceive method .
虽然sendBroadcast我mainActivity工作befor RegisterReceiver,它里面它不会工作。
While sendBroadcast to my mainActivity is working befor RegisterReceiver , inside it it wont work .
我的服务类:
public class SmsReciveService extends IntentService {
public SmsReciveService() {
super("SmsReciveService");
}
public static final String NOTIFICATION = "com.example.Send";
public static ArrayList<String> phoneNumbers = new ArrayList<String>();
public static String messageToSent="";
public static final String hello = "";
public static int numberOfRecives;
public static int numberOfRecivesCurrent;
public static int delay;
public static int sentCounter =0;
public static int notSentCounter =0;
Intent intent2 = new Intent(NOTIFICATION);
@Override
protected void onHandleIntent(Intent intent){
phoneNumbers = (ArrayList<String>) intent.getSerializableExtra("members");
messageToSent = intent.getStringExtra("messageToSent");
sendSMS();
}
public void sendSMS() {
String smsSent = "SEND";
final ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
final SmsManager sm = SmsManager.getDefault();
final ArrayList<String> parts =sm.divideMessage(messageToSent);
final int numParts = parts.size();
for (int j = 0; j < numParts; j++) {
sentIntents.add(PendingIntent.getBroadcast(this, 0, new Intent(smsSent), 0));
}
numberOfRecives=numParts;
delay = numParts*2500;
Intent intent2 = new Intent(NOTIFICATION);
intent2.putExtra(hello, "sending,Number of parts :" + Integer.toString(numParts) );
sendBroadcast(intent2);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
numberOfRecivesCurrent+=1;
Intent intent2 = new Intent(NOTIFICATION);
intent2.putExtra(hello, "test");
sendBroadcast(intent2);
if(numberOfRecivesCurrent==numberOfRecives){
switch (getResultCode()){
case Activity.RESULT_OK: break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:break;
case SmsManager.RESULT_ERROR_NO_SERVICE:break;
case SmsManager.RESULT_ERROR_NULL_PDU:break;
case SmsManager.RESULT_ERROR_RADIO_OFF:break;
}
numberOfRecives+=numParts;
}
}
}, new IntentFilter(smsSent));
for(int i=0; i<phoneNumbers.size(); i++){
Handler hand = new Handler();
final int index=i;
hand.postDelayed(new Runnable() {
@Override
public void run() {
String phone = phoneNumbers.get(index).split(",")[1];
sm.sendMultipartTextMessage(phone,null, parts, sentIntents, null);
}
}, delay*i);
}
}
广播接收器reciver主类中:
BroadcastReceiver reciver inside main class :
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String string = intent.getStringExtra(SmsReciveService.hello);
Toast.makeText(Send.this,string,Toast.LENGTH_LONG).show();
}
};
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
protected void onResume() {
super.onResume();
registerReceiver(receiver, new IntentFilter(SmsReciveService.NOTIFICATION));
}
我在做什么错了?请帮帮忙,招呼。
What i am doing wrong ? Please help,greeting.
推荐答案
本教程完成:
<一个href=\"http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging\">Example:使用消息活动和服务之间的通信
Example: Communication between Activity and Service using Messaging
现在用户可以上网冲浪,或answere在一个电话或什么的。 。 。跟踪和发送短信的是在后台运行,当用户回来,他在网上看到的变化。
now user can go surf the net, or answere a call or whatever . . . the track and sending sms's are working in the background , when user comes back he see the changes online .
这篇关于RegisterReceiver里面IntentService,跟踪SmsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!