所以我有一个BroadcastReceiver和AlarmManager.
假设我像这样创建Pending Intents:
Intent i;
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_1);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent1 = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_2);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent2 = PendingIntent.getBroadcast(context, 2, i, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
并按如下方式安排警报:
now = SystemClock.elapsedRealtime();
long time1 = now + 10 * 1000;
long time2 = time1 + 60 * 1000;
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time1, pendingIntent1);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time2, pendingIntent2);
Run Code Online (Sandbox Code Playgroud)
我现在正在体验我的广播接收器ACTION_1非常可靠地获得广播,而ACTION_2通常没有被传送.所以onReceive很少或永远不会执行持有该动作的意图ACTION_2.怎么回事?我想,*_WAKEUP确保无论如何都要播放广播?
[2015年9月15日更新] - 出于测试目的,我正在尝试在我的onReceive方法中打印出一条日志消息.还是行不通. - 我现在尝试在AlarmManager上使用setExact.还是行不通.- 我甚至尝试过使用过WakefulBroadcastReceiver.还是行不通. - 但我发现,当处于电池充电状态时,设备可靠地唤醒. …
我很长一段时间都在努力解决这个问题.我搜索了解决方案,实际上有一些相关问题的建议,但没有什么真正适合我.所以假设我想创建一个带有(长)消息,复选框和两个按钮的AlertDialog.
// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);
dlg.setMessage(R.string.dlg_msg);
// add checkbox
final CheckBox checkbox = new CheckBox(this);
dlg.setView(checkbox);
// add buttons
dlg.setPositiveButton(...);
dlg.setNegativeButton(...);
// show dialog
dlg.show();
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为如果对话框消息太长,则不会完全显示该复选框.此外,它将完全隐藏在横向模式中.
接下来尝试将查找原始对话框布局文件(对我而言,它位于android-sdk-linux_86/platforms/android-17/data/res/layout/alert_dialog.xml下)并尝试复制相关部分以创建我们的拥有内部对话框布局的"克隆",就像这样:
dialog_checkbox.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:overScrollMode="ifContentScrolls"
android:paddingBottom="12dip"
android:paddingLeft="14dip"
android:paddingRight="10dip"
android:paddingTop="2dip" >
<TextView
android:id="@+id/message"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
</ScrollView>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我们可以尝试将此视图添加到对话框中,如下所示:
// create dialog
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.dlg_title);
// add …Run Code Online (Sandbox Code Playgroud) 只是一个简单的问题:我刚刚发现了这一点
new AlertDialog.Builder(this)
.setTitle("Hi")
.setMessage("Some text. Did you read it?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
})
.setNegativeButton("No, I'm too lazy", null)
.show();
Run Code Online (Sandbox Code Playgroud)
在我的设备上运行良好.传递null作为DialogInterface.OnClickListener只是解除对话框.我想问一下,如果这是合法的,或者可能会导致问题,例如旧设备/ Android版本?我没有在文档中找到任何内容.
直到现在我才这样做,但它保存了一些代码并且在我看来是有意义的.到目前为止,我总是创建一个OnClickListener来关闭对话框.看来有点不必要.
我做了很多研究,这似乎是许多用户的常见错误,但原因却截然不同。我发现这些都不适合我。
我越来越
java.lang.RuntimeException: Unable to start activity ComponentInfo{
[...]/[...].activities.StartActivity}: android.view.InflateException:
Binary XML file line #173: Error inflating class [...].BannerAd
[...]
Caused by: android.view.InflateException: Binary XML file line #8: Error
inflating class com.google.android.gms.ads.AdView
[...]
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.google.android.gms.ads.AdView" on path: DexPathList[[zip file
"/data/app/[...]-1.apk"],nativeLibraryDirectories=[/data/app-lib/[...]-1,
/vendor/lib, /system/lib]]
Run Code Online (Sandbox Code Playgroud)
我安装了最新版本的 ADT 和 SDK 软件包。我将 google-play-services_lib 复制到我的工作区并将其导入为 Android 项目。我将它作为库添加到我的应用程序项目中。我检查了“订单和导出”下的所有内容。
我有一个banner_ad.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="[...]" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
以及我正在使用的 BannerAd.java:
package [...];
import android.content.Context;
import …Run Code Online (Sandbox Code Playgroud) 我通过以下方式主题:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="alertDialogTheme">@style/AppTheme.Dialog</item>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/accent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是,当我在我的应用程序中创建AlertDialogs时,它们根本不是主题.其他组件如ActionBar正在正确着色.我错过了什么吗?
我使用的是AppCompat版本com.android.support:appcompat-v7:23.1.1,我的设备安装了Android 4.4.4.