我正在尝试从我的服务类中启动浏览器,但它不起作用。下面是我的服务类的onStartCommand方法的示例代码:
Toast.makeText(context, "Service called", Toast.LENGTH_SHORT).show();
final MyDBHandler dbHandler = new MyDBHandler(context, null, null, 1);
final Context cont = context;
String expectedPattern = "MMM d, yyyy";
sdf = new SimpleDateFormat(expectedPattern);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
Toast.makeText(cont, "In service handler", Toast.LENGTH_SHORT).show();
dbContacts = dbHandler.getAllContacts();
for(Contact cn : dbContacts) {
Toast.makeText(cont, "Checking contacts...", Toast.LENGTH_SHORT).show();
String url = "http://www.example.com/send_sms?from=123456789&to=";
final String phone = cn.getContactphone();
String dob = cn.getContactdob();
dobDate = sdf.parse(dob);
today = new Date();
long diffInMs = today.getTime() - dobDate.getTime();
int diff = (int) diffInMs / (1000 * 60 * 60 * 24);
Toast.makeText(cont, "Diff is " + diff, Toast.LENGTH_SHORT).show();
if(diff == 0) {
String smsBody = "Welcome message";
String SMS_SENT = "SMS_SENT";
String SMS_DELIVERED = "SMS_DELIVERED";
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(cont, 0, new Intent(SMS_SENT), 0);
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(cont, 0, new Intent(SMS_DELIVERED), 0);
// When SMS has been sent
cont.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context cont, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(cont, "SMS sent successfully to " + phone, Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(cont, "Generic failure cause", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(cont, "Service is currently unavailable", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(cont, "No pdu provided", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(cont, "Radio was explicitly turned off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SMS_SENT));
// When SMS has been delivered
cont.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context cont, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(cont, "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(cont, "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SMS_DELIVERED));
// Get default instance of SmsManager
SmsManager smsManager = SmsManager.getDefault();
// Send a text-based SMS
Toast.makeText(cont, "About to launch browser", Toast.LENGTH_SHORT).show();
url += phone + "&msg=" + smsBody;
Toast.makeText(cont, "URL is " + url, Toast.LENGTH_LONG).show();
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
Runnable r = new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread stephThread = new Thread(r);
stephThread.start();
stopSelf();
return START_STICKY;
最后一个Toast显示正确的URL,但它不会启动浏览器。
我怎么能纠正这个?如何让浏览器与后台进行交互,即调用url但是浏览器不会弹出?
提前致谢!
答案 0 :(得分:0)
我在link找到了解决方案
编译所需的apache配置后,我需要使用AsyncTask,如下所示(在doInBackground()方法中):
try {
URL url = new URL(params[i]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStream it = new BufferedInputStream(urlConnection.getInputStream());
InputStreamReader read = new InputStreamReader(it);
BufferedReader buff = new BufferedReader(read);
StringBuilder dta = new StringBuilder();
String chunks ;
while((chunks = buff.readLine()) != null)
{
dta.append(chunks);
}
}
else {
//Do else here
}
} catch (Exception e) {
e.printStackTrace();
}