小编New*_*Dev的帖子

如何检测通知是否已被解除?

Android中是否有任何方法可以检测用户何时向左侧滑动通知并将其删除?我正在使用闹钟管理器来设置重复警报,当用户取消通知时,我需要停止重复警报.这是我的代码:

设置重复提醒:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);
Run Code Online (Sandbox Code Playgroud)

我的通知代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the notification ID.
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");

    //Get the name of the reminder.
    String reminderName = getIntent().getExtras().getString("Reminder_Name");

    //PendingIntent stores the Activity that should be launched when the user taps the notification.
    Intent i = new Intent(this, ViewLocalRemindersDetail.class);
    i.putExtra("NotifID", notifID);
    i.putExtra("notification_tap", true);

    //Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
    PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, …
Run Code Online (Sandbox Code Playgroud)

android repeat alarm alarmmanager notificationmanager

24
推荐指数
1
解决办法
1万
查看次数

设备方向的陀螺仪问题

我使用本教程从设备的陀螺仪获得俯仰和滚动数据:http://www.thousand-thoughts.com/2012/03/android-sensor-fusion-tutorial/

所有读数都非常准确(有一个滤波器应用于教程中的代码以消除陀螺漂移).不幸的是,只有当我的设备平放在与地面平行的表面上时,代码才有效.我的应用程序工作的最理想位置是设备的顶部指向正面(即,设备垂直于地面,屏幕面向用户).每当我将设备定位在此位置时,音高值将达到+90度(如预期的那样).我想要做的是将此位置设置为我的设备的0度点(或初始位置),以便当我的设备直立(纵向模式)且屏幕朝向用户时,音高读数为0度.

我在这个问题的帮助下问了教程的作者,他回答说:

"如果你想将直立位置作为初始位置,你必须相应地旋转你的参考框架.最简单的方法是将产生的旋转矩阵绕x轴旋转-90度.但你必须请注意算法中的哪一点应用这个旋转.永远记住旋转不是可交换操作.为了更具体这一点,我将不得不再次检查代码,因为我还没有使用它一段时间现在."

我真的很困惑,并且难以理解如何旋转我的参考框架.我想底线是我不知道如何围绕x轴旋转矩阵-90度.如果有人可以帮我解决这个问题,那就太棒了.这是我的代码,以防任何人想要引用它:

public class AttitudeDisplayIndicator extends SherlockActivity implements SensorEventListener {

private SensorManager mSensorManager = null;

// angular speeds from gyro
private float[] gyro = new float[3];

// rotation matrix from gyro data
private float[] gyroMatrix = new float[9];

// orientation angles from gyro matrix
private float[] gyroOrientation = new float[3];

// magnetic field vector
private float[] magnet = new float[3];

// accelerometer vector
private float[] accel = new float[3];

// orientation angles from accel …
Run Code Online (Sandbox Code Playgroud)

android accelerometer gyroscope android-sensors

8
推荐指数
1
解决办法
9843
查看次数

如何将Java.util.calendar设置为将来的特定时间段

我正在使用Java的日历在特定的日期和时间设置闹钟.我知道如何在用户选择特定日期和时间时使其工作.例如,如果用户想要在2013年7月17日上午10:45设置闹钟,我使用以下代码:

//Get the calendar instance.
Calendar calendar = Calendar.getInstance();       

//Set the time for the notification to occur.
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 17);                 
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
Run Code Online (Sandbox Code Playgroud)

当我想在特定日期和时间设置闹钟时,上述所有代码都能正常工作.我的问题是,如何设置一个日历实例,用户用户希望在当前日期和时间20分钟后发出警报?因此,如果当前时间是下午6:50,我需要在下午7:10发出警报.如何以编程方式设置?

我尝试通过Java.util.calendar的内置方法获取当前日期和时间,并尝试向Calendar.MINUTE变量添加20分钟.但是,如果当前时间距离午夜不到20分钟(日期会发生变化),或者距离另一个小时20分钟(小时会发生变化),我认为这不会起作用.我怎样才能解决这个问题?感谢你的帮助!

android calendar

7
推荐指数
1
解决办法
2万
查看次数

获取选定的ListView项目ID(使用SQLite DB填充的数据)

我有一个数据库,可以跟踪用户的提醒数据(提醒名称,注释,日期,时间等).第一列是它的主键(_ID).ListView是从数据库中填充的,它显示了一组带有提醒名称的简单行,如下所示:


把垃圾带出去.


遛狗


吃午餐.


等等


现在我的问题是,如何让我的应用程序找出点击了哪个特定提醒?单击一行时,我需要从我的数据库中找到其主键(_ID列),并能够检索该行中的所有数据.

到目前为止,我知道我需要使用onItemClick来检测点击.但是,如何获取已单击的项目的主键值(_ID)?我当前的代码如下所示:

final Context context = this;

//DB Connectivity variables.
protected RemindersDAO remindersDAO;
protected SimpleCursorAdapter remindersCursorAdapter;
public ListView viewRemindersListView;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_local_reminders);

    // Get rid of the app title in the action bar.
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);

    viewRemindersListView = (ListView) findViewById(R.id.listview_view_local_reminders);

    remindersDAO = new RemindersDAO(this);
    Cursor cursor = remindersDAO.all(this);

    remindersCursorAdapter = new SimpleCursorAdapter(this,
                                     R.xml.view_reminders_item_layout,
                                     cursor, new String [] { RemindersDAO.NAME },
                                     new int[] { R.id.view_reminders_item_text } );

    viewRemindersListView.setAdapter(remindersCursorAdapter); …
Run Code Online (Sandbox Code Playgroud)

database sqlite android listview

4
推荐指数
1
解决办法
1万
查看次数

是从一个单独的线程调用的方法,在调用线程上运行?

我已经使用线程2-3天了,我有一个关于方法的快速问题.我正在创建一个Android应用程序,它从主UI线程开始(为了清楚起见,我们将其称为"UI线程").我正在使用以下代码生成一个新线程:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        someMethod();
    }

});
thread.start();
Run Code Online (Sandbox Code Playgroud)

我的问题是,someMethod()还会运行我刚创建的新线程,因为我从那里调用它?或者它会在UI线程上运行吗?作为参考,someMethod()位于创建新线程的方法之外.

如果someMethod()不能在新线程上运行,我该怎么做呢?谢谢.

java methods multithreading

4
推荐指数
1
解决办法
1909
查看次数

如何逆时针旋转3D矩阵90度?

我试图用Java逆时针旋转矩阵90度.我找到了如何用2D矩阵做到这一点的答案,但我的矩阵是3D.

以下是我如何进行2D旋转的方法:

static int[][] rotateCW(int[][] mat) {
    final int M = mat.length;
    final int N = mat[0].length;
    int[][] ret = new int[N][M];
    for (int r = 0; r < M; r++) {
        for (int c = 0; c < N; c++) {
            ret[c][M-1-r] = mat[r][c];
        }
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

那我怎么去旋转3D矩阵呢?

java rotation matrix rotational-matrices

2
推荐指数
1
解决办法
6961
查看次数

尝试实现RemoteControlClient,但仍然没有获得锁屏控件

我已经浏览了SDK(RandomMusicPlayer)提供的RemoteControlClient示例程序.但是,我不能为我的生活弄清楚如何使用我自己的音乐播放器获得锁屏控制RemoteControlClient.这就是我的音乐播放器服务:

//Request audio focus for playback
int result = audioManager.requestAudioFocus(audioFocusChangeListener,
                                            AudioManager.STREAM_MUSIC,
                                            AudioManager.AUDIOFOCUS_GAIN);

//Check if audio focus was granted. If not, stop the service.
if (result!=AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    //Stop the service.
    stopSelf();
    Toast.makeText(mContext, R.string.close_other_audio_apps, Toast.LENGTH_LONG).show();
}

ComponentName remoteControlsReceiver = new ComponentName(getPackageName(),      
                                         HeadsetButtonsReceiver.class.getName());

if (mRemoteControlClientCompat == null) {
   Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
   remoteControlIntent.setComponent(remoteControlsReceiver);

   mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this, 0, remoteControlIntent, 0));
   RemoteControlHelper.registerRemoteControlClient(audioManager, mRemoteControlClientCompat);
   audioManager.registerMediaButtonEventReceiver(remoteControlsReceiver);
}
Run Code Online (Sandbox Code Playgroud)

这基本上就是SDK示例所做的(据我所知).我已经确认我的应用程序能够获得音频焦点.我完成了我的作业,并且知道需要音频专注才能使用.我到底错过了什么?任何指向正确方向的人都非常感激.:)

android remote-control media-player lockscreen

2
推荐指数
1
解决办法
1245
查看次数

Getter和Setters不工作?

我现在有两个班:RemindersDAO.java和ViewLocalReminders.java.

我正在尝试访问ViewLocalReminders.java中的变量,我试图从RemindersDAO.java中调用它.我是通过使用getter/setter方法组合来做到这一点的.但是,由于某种原因,我的变量值在getter方法中始终设置为0.这是代码:

ViewLocalReminders.java

public class ViewLocalReminders extends SherlockListActivity {

private long reminderID;

    public void onCreate(Bundle SavedInstance) {

    reminderID = 16;
    setReminderID(reminderID);      

    }

    public void setReminderID(long id) {
        id = reminderID;
        System.out.println("Reminder ID Value in Setter: " + reminderID);
    }

    public long getReminderID() {
        return reminderID;
        System.out.println("Reminder ID Value in Getter: " + reminderID);
    }

}
Run Code Online (Sandbox Code Playgroud)

RemindersDAO.java

public class RemindersDAO extends SQLiteOpenHelper {

    @SuppressWarnings("deprecation")
    public Cursor getRowByID(Activity activity) {
    String[] from = { _ID, NAME };

    ViewLocalReminders viewLocalReminders = new ViewLocalReminders(); …
Run Code Online (Sandbox Code Playgroud)

java android getter-setter

1
推荐指数
1
解决办法
5944
查看次数

如何处理内置存储和外部SD卡?

好的,所以我尽可能多地进行研究,试图弄清楚我做错了什么,但谷歌或Android教程中的任何内容都无法在我的场景中真正回答我.

场景:

假设我的Android设备配有16GB的内置存储空间.我们还假设该设备具有32GB SD标准.基本上,该设备总共有48GB的存储空间.我的应用需要扫描设备上每个用户可访问的位置.换句话说,我需要能够访问和读取普通用户(非root用户)可以读取和写入的每个位置.

我现在在做什么:

我正在使用推荐的Environment.getExternalStorageDirectory()指向我的/mnt/sdcard.

问题:

是否Environment.getExternalStorageDirectory()允许我访问设备上的每个用户可访问的存储?我很困惑,因为可以有16GB的内置存储和32GB的SD卡存储,但Android如何处理这两个独立的位置?它是简单地将两者合并为单数/mnt/sdcard还是我只能用我的代码行访问两个存储目录中的一个?

很抱歉,如果这听起来像一个菜鸟问题,但是来自iPhone背景,其中唯一可用的存储是内部存储,我真的被Android处理这种情况的方法所困扰.感谢你的帮助!

storage android sd-card

1
推荐指数
1
解决办法
2514
查看次数