小编vit*_*han的帖子

开发报警应用

我想开发一个报警应用程序.应用程序应该像这样工作:

  • 发动它
  • 活动告诉我时间
  • 我可以设置闹钟
  • 我可以关闭申请
  • 当闹钟时间到来时,它会启动一个活动(即使设备被锁定)

我已经尝试调整此示例https://github.com/commonsguy/cwac-wakeful但是在闹钟时间到来时我无法启动活动.

我使用此代码来设置警报(对于测试我已将此代码插入onCreate到活动方法中):

Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);
Run Code Online (Sandbox Code Playgroud)

这是OnAlarmReceiver类:

public class OnAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
        WakefulIntentService.sendWakefulWork(context, AlarmService.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是服务类:

public class AlarmService extends WakefulIntentService {

    public AlarmService(String name) {
        super(name);
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        Log.i(ClockActivity.LOG_TAG, …
Run Code Online (Sandbox Code Playgroud)

android alarmmanager wakelock commonsware

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

无法在额外的属性扩展上获取属性'compileSdkVersion',因为它不存在Open File

我将从GitHub下载的项目作为模块导入到我的Android Studio项目中."导入模块..."向导工作正常,但当Adroid Studio尝试重建项目时,它返回给我这个错误:

Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
Run Code Online (Sandbox Code Playgroud)

该错误与导入模块的"build.gradle"文件中的此行相关:

compileSdkVersion rootProject.compileSdkVersion
Run Code Online (Sandbox Code Playgroud)

我试图在项目"build.gradle"中添加"ext"部分,如下所示:

ext {
    compileSdkVersion 26
}
Run Code Online (Sandbox Code Playgroud)

但是这样我收到一个新的错误:

Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ... 
Run Code Online (Sandbox Code Playgroud)

android android-studio build.gradle android-gradle-plugin gradle-plugin

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

用它来计算SpannableString

我正在运行时构建一个将放入TextView的文本.本文由不同大小的字体组成.我必须计算此文本的宽度(以像素为单位).我曾尝试使用Paint.measureText,但它没有考虑不同的字体大小.如何计算实际宽度?

这是一个例子:

LinearLayout text = (LinearLayout) findViewById(R.id.LinearLayout);

SpannableStringBuilder str = new SpannableStringBuilder("0123456789");
str.setSpan(new RelativeSizeSpan(2f), 3, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView tmp = new TextView(this);
tmp.setText(str,BufferType.SPANNABLE);
text.addView(tmp);

Float dim = tmp.getPaint().measureText(str, 0, str.length());
Run Code Online (Sandbox Code Playgroud)

在此示例中,如果我将相对大小设置为"2f"或"3f"(例如),则返回"MeasureText"的总大小是相同的.

谢谢

android textview

5
推荐指数
2
解决办法
3451
查看次数