我想使用Datepicker和Timepicker设置我的时间和日期,但是当我按下按钮时应用程序崩溃了。这是代码:
public class MainActivity extends Activity {
DatePicker datePicker;
TimePicker timePicker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
datePicker = (DatePicker) findViewById(R.id.datePicker);
}
public void btnClick(View view) {
Toast.makeText(getBaseContext(),
"Date selected:" + (datePicker.getMonth() + 1) +
"/" + datePicker.getDayOfMonth() +
"/" + datePicker.getYear(),
Toast.LENGTH_SHORT).show();
NumberFormat formatter = new DecimalFormat("00");
Toast.makeText(getBaseContext(),
"Time selected:" +
timePicker.getCurrentHour() +
":" + formatter.format(timePicker.getCurrentMinute()),
Toast.LENGTH_SHORT).show();
Calendar c = Calendar.getInstance();
c.set(datePicker.getYear(),Calendar.SEPTEMBER,datePicker.getDayOfMonth(), timePicker.getCurrentHour(),timePicker.getCurrentMinute(), 00);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());
}
}
这就是表现:
<permission android:name="android.permission.SET_TIME"/>
其他问题是如何在我的代码中设置月份? set()
方法不接受datePicker.getMonth()
。
日志:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3044)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14109)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14109)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: setTime: Neither user 10050 nor current process has android.permission.SET_TIME.
at android.os.Parcel.readException(Parcel.java:1327)
at android.os.Parcel.readException(Parcel.java:1281)
at android.app.IAlarmManager$Stub$Proxy.setTime(IAlarmManager.java:237)
at android.app.AlarmManager.setTime(AlarmManager.java:290)
at net.learn2develop.usingdatepicker.MainActivity.btnClick(MainActivity.java:45)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14109)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的设备应 rooted 。
请在androidmanifest.xml中添加权限
<permission android:name="android.permission.SET_TIME"
android:protectionLevel="signature|system"
android:label="@string/permlab_setTime"
android:description="@string/permdesc_setTime" />
现在试试。
答案 1 :(得分:0)
在setTime()
上呼叫AlarmManager
需要SET_TIME
权限,而您无法将其作为第三方应用开发者。这是系统权限,您的应用程序需要是使用平台签名密钥签名的系统应用程序才能获得它。或者你需要成为root。