我第一次尝试使用 Codename one for android 的本机界面。我尝试下面的代码在特定时间后在后台获取服务器上的数据存储,即使应用程序未运行也是如此。我不考虑是否启用数据服务/wifi。为此,我通过 Codename one 创建了一个本机实现。在谷歌这么多并尝试了太多方法之后,仍然Looper.prepare();
会在处理程序中调用相同的错误。我尝试过的代码之一service
如下。我不是android程序员,所以请不要认为我是android专家。
本机 Impl 中的代码:
public class NativeCallImpl {
public void setNative(String param) {
implCall ic = new implCall();
ic.Native(param);
}
public boolean isSupported() {
return true;
}
}
impl 调用类:
class implCall extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //To change body of generated methods, choose Tools | Templates.
}
public void Native(String param) {
try {
Toast.makeText(this, param, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), UploadData.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTime().getTime(), 10000, pendingIntent);
//startService(intent);
} catch (Exception e) {
Toast.makeText(this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Intent 中调用的类:
public class UploadData extends Service {
@Override
public void onStart(Intent intent, int startid) {
//return super.onStartCommand(intent, flags, startId); //To change body of generated methods, choose Tools | Templates.
try {
String data = URLEncoder.encode("mobileNumber", "UTF-8") + "="
+ URLEncoder.encode(user.user.getMobileNumber(), "UTF-8");
data += "&" + URLEncoder.encode("EventName", "UTF-8")
+ "=" + URLEncoder.encode("na", "UTF-8");
data += "&" + URLEncoder.encode("Status", "UTF-8")
+ "=" + URLEncoder.encode("auto", "UTF-8");
data += "&" + URLEncoder.encode("Latitude", "UTF-8")
+ "=" + URLEncoder.encode("lat", "UTF-8");
data += "&" + URLEncoder.encode("Longitude", "UTF-8")
+ "=" + URLEncoder.encode("long", "UTF-8");
data += "&" + URLEncoder.encode("Address", "UTF-8")
+ "=" + URLEncoder.encode("na", "UTF-8");
data += "&" + URLEncoder.encode("imageCode", "UTF-8")
+ "=" + URLEncoder.encode("na", "UTF-8");
URL url = new URL("http://example.com/appFiles/checkInOut/checkInOut.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
} catch (Exception ex) {
//Toast.makeText(null, ex.toString(), Toast.LENGTH_LONG).show();
System.out.println(ex.getMessage());
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
错误信息 :