我第一次尝试使用Codename one for native的原生界面。我尝试下面的代码在特定时间后在后台获取数据存储,即使应用程序没有运行。我没有考虑是否启用了数据服务/ wifi。为了完成这项工作,我通过Codename创建了一个本机实现。谷歌这么多,并尝试太多的方法仍然得到相同的错误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 call class:
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;
}
}
错误讯息:
答案 0 :(得分:1)
正如@chen所提到的,你应该使用builtin local notifications并且不需要本机代码。
这也适用于iOS,所以它要好得多。手头的异常被调用是因为你使用Codename One线程而不是你需要从活动中用runOnUiThread
打包的Android线程。