用于设置呼叫活动的日期。 我使用报警管理器和按钮点击。
活动 包com.example.nn.gallery;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class SettingsPanel extends Activity {
Long time;
Button button;
public static final String ALARM_TIME_HOUR = "alarm_time_hour";
public static final String ALARM_TIME_MINUTES = "alarm_time_minets";
public static final String ALARM_NOTIFICATION_SECONDS = "alarm_notification_seconds";
TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_panel);
timePicker = (TimePicker) findViewById(R.id.timePicker);
button = (Button) findViewById(R.id.saveDate);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//вычислить время запуска
//установить alarm
int hour = timePicker.getHour();
int minutes = timePicker.getMinute();
long acrossTimeMs = getAlarmTime(hour,minutes);
//System.out.println(acrossTimeMs);
//savePickerTime(hour,minutes,acrossTimeMs);
restartAlarm(acrossTimeMs);
}
});
}
/**
*
* @param hour
* @param minutes
*/
public void savePickerTime(int hour,int minutes,long accrosTime) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(ALARM_TIME_HOUR,hour);
editor.putInt(ALARM_TIME_MINUTES,minutes);
editor.putLong(ALARM_NOTIFICATION_SECONDS,accrosTime);
editor.commit();
Toast.makeText(this, "Notification time saved", Toast.LENGTH_SHORT).show();
}
/**
* setAlarmTime
* @param alarmTime ms unixtime
*/
public void restartAlarm(long alarmTime){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT );
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
/**
* Нow many minutes will call
*
* @param pickerHour
* @param pickerMinutes
* @return long millseconds
*/
public long getAlarmTime(int pickerHour,int pickerMinutes){
Calendar c = Calendar.getInstance();
int currentHour = c.get(Calendar.HOUR_OF_DAY);
int currentMinutes = c.get(Calendar.MINUTE);
//System.out.println("current " + currentHour + " " + currentMinutes);
//System.out.println("picker " + pickerHour + " " + pickerMinutes);
//System.out.println((currentHour * 60) + currentMinutes);
//System.out.println((pickerHour * 60) + pickerMinutes);
boolean currentTimeMore = ((currentHour * 60) + currentMinutes) > ((pickerHour * 60) + pickerMinutes);
//System.out.println( ((currentHour * 60) + currentMinutes) + " " + ((pickerHour * 60) + pickerMinutes));
int across;
if(currentTimeMore){
across = (24 * 60 )- ((currentHour * 60) + currentMinutes) + ((pickerHour * 60) + pickerMinutes);
}else {
across = (((pickerHour * 60) + pickerMinutes) - ((currentHour * 60) + currentMinutes));
}
long unixTime = System.currentTimeMillis();
//across = unixTime + (across * 60);
long acrossLong = unixTime + (across * 60 * 1000);
return acrossLong;
//System.out.println(across + " across minets");
//System.out.println(timeEcuals);
//получить текущь время
//получить время пикера
//вернуть лонг милисекунды
}
}
AlarmReceiver
package com.example.nn.gallery;
/**
* Created by nn on 6/2/16.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("com.example.nn.gallery", "com.example.nn.gallery.CallCamera");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
布局
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_settings_panel" />
</android.support.design.widget.CoordinatorLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nn.gallery">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsPanel"
android:label="@string/title_activity_settings_panel"
android:theme="@style/AppTheme.NoActionBar" />
<receiver android:name=".AlarmReceiver" />
<activity android:name=".CallCamera"></activity>
</application>
</manifest>
我如何在手机上看到错误日志? 模拟器不会在您的计算机上产生错误。