我正在我的Android应用程序中执行由GCM触发的推送通知.我想显示通知,如果应用程序在警告框中运行,如果应用程序未运行意味着不在前台只显示状态栏通知,您如何确定应用程序是否正在运行并且在前台?是这可以显示这样的通知.现在使用此代码显示状态栏推送通知
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, BottomActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud) 我有一个列表,我需要自定义排序,然后转换为具有其Id与名称映射的地图.
这是我的代码:
Map<Long, String> map = new LinkedHashMap<>();
list.stream().sorted(Comparator.comparing(Building::getName)).forEach(b-> map.put(b.getId(), b.getName()));
Run Code Online (Sandbox Code Playgroud)
我认为这将完成这项工作,但我想知道我是否可以避免LinkedHashMap
在这里创建并使用花哨的函数式编程来完成一行中的工作.
我如何使用收集器将DAO列表转换为 Map<String, List<Pojo>>
daoList看起来像这样:
[0] : id = "34234", team = "gools", name = "bob", type = "old"
[1] : id = "23423", team = "fool" , name = "sam", type = "new"
[2] : id = "34342", team = "gools" , name = "dan", type = "new"
Run Code Online (Sandbox Code Playgroud)
我想对“团队”属性进行分组,并为每个团队提供一个列表,如下所示:
"gools":
["id": 34234, "name": "bob", "type": "old"],
["id": 34342, "name": "dan", "type": "new"]
"fool":
["id": 23423, "name": "sam", "type": "new"]
Run Code Online (Sandbox Code Playgroud)
Pojo看起来像这样:
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Pojo{
private String …
Run Code Online (Sandbox Code Playgroud) 我有两个Android应用程序具有类似的功能,但不同的drawables和布局.
我想将所有代码和资源放在库项目中,并覆盖引用该库的两个应用程序中的一些drawable和布局.
我创建了一个引用该库的应用程序.我将所有资源从库复制到应用程序并更改了一些drawable.我也改变了一些字符串资源.当我在Eclipse中的布局编辑器中查看其中一个更改的布局时,我看到了正确的重写图像.
当我启动应用程序时,我看到我在应用程序中更改的字符串资源正确显示(库字符串被应用程序的资源覆盖),但我的ImageView
s 的drawables 都取自库的资源.
我还对一些布局资源进行了更改(移动并调整了一些图像的大小).当我启动使用更改的布局的活动(源代码在库中的活动)时,我看到带有旧(库)drawable的新(应用程序)布局.
我尝试ImageView
用两种方式定义s 的drawable :
在布局xml中: android:src="@drawable/image_id"
在活动的代码中:
ImageView view = (ImageView)findViewById(R.id.viewId);
view.setImageResource(R.drawable.image_id);
Run Code Online (Sandbox Code Playgroud)在这两种情况下,我看到的图像都来自库项目的资源,而不是应用程序.
如果我无法解决这个问题,我将不得不复制这两个应用程序中的所有代码,这将是一个耻辱.
难道我做错了什么?
我的服务器只运行一个发送推送通知的版本,它有我的生产(发布)推送证书.
如何使用相同的生产证书在我的调试设备上测试通知并调试版本?
我的设备正在发送令牌,但是当我尝试发送真实的推送通知时,Apple服务器返回状态8错误,这意味着设备令牌不正确.
为什么这不起作用?我得到编译错误"无法对非静态方法打印静态引用..."
public class Chapter3 {
public void print(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Arrays.asList("a", "b", "c").forEach(Chapter3::print);
}
}
Run Code Online (Sandbox Code Playgroud) 下面是我写过的第一个Java泛型:
public class MyClass {
public static <T> T castToAnotherType(Object param) {
T ret = null;
try {
ret = (T) param;
} catch (ClassCastException e) {
System.out.print("Exception inside castToAnotherType()");
}
return ret;
}
public static void main(String[] args) {
try {
String obj = MyClass.castToAnotherType(new Object());
} catch (ClassCastException e) {
System.out.print("Exception outside castToAnotherType()");
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果是"castToAnotherType()之外的异常".为什么在泛型方法中不会发生异常?
我正在寻找一种以Stream
干净的方式优化处理的方法.
我有类似的东西:
try (Stream<Path> stream = Files.list(targetDir)) {
Map<String, List<Path>> targetDirFilteredAndMapped = stream.parallel()
.filter(path -> sd.containsKey(md5(path)))
.collect(Collectors.groupingBy(path -> md5(path)));
} catch (IOException ioe) { // manage exception }
Run Code Online (Sandbox Code Playgroud)
由于该md5
功能非常昂贵,我想知道是否有办法每个文件只调用一次.
有什么建议?
首先,请不要关闭它作为什么NullPointerException
以及如何解决它的副本.我知道是什么NullPointerException
,我知道如何在我自己的代码中解决它,但不是当它被mysql-connector-java-5.1.36-bin.jar抛出时,我无法控制.
我们在mySQL数据库上运行公共数据库查询时遇到异常,该查询在大多数情况下都有效.我们在部署新版本后开始看到此异常,但发生它的查询在很长一段时间内没有发生变化.
以下是查询的外观(通过一些必要的简化).我用之前和之后执行的一些逻辑包围了它.实际的代码不是全部都在一个单独的方法中,但我把它放在一个块中,以便更容易理解.
Connection conn = ... // the connection is open
...
for (String someID : someIDs) {
SomeClass sc = null;
PreparedStatement
stmt = conn.prepareStatement ("SELECT A, B, C, D, E, F, G, H FROM T WHERE A = ?");
stmt.setString (1, "someID");
ResultSet res = stmt.executeQuery ();
if (res.next ()) {
sc = new SomeClass ();
sc.setA (res.getString (1));
sc.setB (res.getString (2));
sc.setC (res.getString (3));
sc.setD (res.getString (4));
sc.setE (res.getString (5)); …
Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题:
我创建了这个我需要映射到的流Map<String, Object>
:
private Map<String, Object> collectArguments(JoinPoint point) {
CodeSignature signature = (CodeSignature) point.getSignature();
String[] argNames = signature.getParameterNames();
Object[] args = point.getArgs();
return IntStream.range(0, args.length)
.collect(Collectors.toMap(param -> argNames[param], param -> args[param]));
}
Run Code Online (Sandbox Code Playgroud)
我收到以下消息,我不太清楚:
[Java] Type mismatch: cannot convert from Collector<Object,capture#3-of ?,Map<Object,Object>> to Supplier<R>
Run Code Online (Sandbox Code Playgroud) java ×7
java-8 ×5
java-stream ×3
android ×2
generics ×2
collections ×1
collectors ×1
drawable ×1
ios ×1
ios7 ×1
jdbc ×1
mysql ×1
xcode5 ×1