小编jus*_*ser的帖子

android显示任何应用程序顶部弹出窗口的通知

使用下面的代码,我的通知只会添加到通知栏中,不会显示弹出样式消息,就像您在另一个应用程序中收到whatsapp消息一样.是什么让这种情况发生在通知中?

private void sendNotification(int distance, ViewObject viewObject) {
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("path", viewObject.getPath());
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle()));
    bigText.setBigContentTitle(getString(R.string.hello));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_wald_poi)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
            .setColor(getResources().getColor(R.color.primary))
            .setContentTitle(getString(R.string.hello))
            .setContentIntent(notificationPendingIntent)
            .setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(bigText);

    builder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, builder.build());
}
Run Code Online (Sandbox Code Playgroud)

android

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

PreferenceActivity Android 4.0及更早版本

在ApiDemos for Android 4.0中尝试不同的首选项活动,我在代码中看到,例如,在PreferencesFromCode.java中不推荐使用某些方法.

所以我的问题是:如果我使用PreferenceFragment,它是适用于所有版本还是仅适用于3.0或4.0及更高版本?

如果是这样,我应该使用哪种适用于2.2和2.3呢?

android preferenceactivity

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

检测手机与平板电脑

有没有办法检测用户是使用平板电脑还是手机?例如,一个人使用平板电脑(任何版本为3+和iPad的安卓平板电脑)上网,他们肯定会喜欢查看与坐在桌面计算机上的人相同的未被剥离的版本.而手机冲浪者肯定更喜欢网站的精简版本,因为它加载速度更快,并且可能更容易用拇指导航.这可以通过检查此处找到的userAgent oct屏幕宽度来完成:

在jQuery中检测移动设备的最佳方法是什么?

但问题来自像谷歌Galaxy Nexus这样的手机,它具有与平板电脑相同的分辨率,但只有屏幕尺寸的一半.在我看来,即使分辨率很高,显示移动版也会更好,因为屏幕很小.

有没有办法检测到这一点,还是我必须做出妥协?

javascript jquery html5 css3

22
推荐指数
2
解决办法
3万
查看次数

Java日历获取当前日期,没有小时,分钟,秒和毫秒,以毫秒为单位

我想以毫秒为单位获取当前日期,只有年,月和日期.但是当我使用这段代码时:

Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
currentDate = cal.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)

我仍然以毫秒为单位获得时间.我怎样才能解决这个问题?

//安德烈

java calendar

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

类型不匹配推断的类型是 () -> Unit 但预期 FlowCollector<Int>

当试图从 Flow 中收集时,类型突然不匹配,它正在工作,然后突然启动。

在我的视图模型中:

class MyViewModel: ViewModel() {

    lateinit var river: Flow<Int>

    fun doStuff() {
        river = flow {
            emit(1)
        }.flowOn(Dispatchers.Default)
        .catch {
            emit(0)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的活动中,我有以下内容:

lifecycleScope.launch {
    viewModel.river.collect { it ->
        // this whole collect is what has the error. 
    }
}
Run Code Online (Sandbox Code Playgroud)

collect给出了错误:Type mismatch: inferred type is () -> Unit but FlowCollector<Int> was expected

这怎么会发生?

android kotlin kotlin-coroutines kotlin-flow

20
推荐指数
2
解决办法
1532
查看次数

Android状态栏通知 - 意图第二次获得旧版附加功能

当我创建通过C2DM发送并在我的应用程序中收到的通知时,我想传递一些来自C2DM的推送通知附带的数据.这在我第一次打开通知时工作正常.然后根据活动的状态使用onNewIntent或onCreate接收数据.

但是如果我通过C2DM发送第二个推送通知,则会使用新数据正确接收,但是当从意图中获取额外内容时,我仍然可以从上一个消息中获取数据.我希望看到的标题和数据在通知中正确显示.所以我的意图一定是错的.如果活动正在运行而不是,则会发生这种情况.

要创建通知,请执行以下操作:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, DesktopApp.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("msg_id", msg_id);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent);
notificationManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud)

然后阅读意图:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent myIntent = getIntent(); // this is just for example purpose
    int i = myIntent.getIntExtra("msg_id", -1); …
Run Code Online (Sandbox Code Playgroud)

notifications android push-notification android-intent

19
推荐指数
2
解决办法
8009
查看次数

OkHttp javax.net.ssl.SSLPeerUnverifiedException:未验证主机名domain.com

我一直在努力让这个工作.我正在尝试使用自签名证书通过https连接到我的服务器.我不认为现在还没有任何页面或示例.

我做了什么:

  1. 按照本教程创建了bks密钥库:http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

它用于openssl s_client -connect domain.com:443从服务器获取证书.然后使用充气城堡创建一个bks密钥库.

  1. 从原始文件夹中读取创建的密钥库,将其添加到sslfactory,然后再添加到OkHttpClient.像这样:

    public ApiService() {
        mClient = new OkHttpClient();
        mClient.setConnectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        mClient.setReadTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        mClient.setCache(getCache());
        mClient.setCertificatePinner(getPinnedCerts());
        mClient.setSslSocketFactory(getSSL());
    }
    
    protected SSLSocketFactory getSSL() {
        try {
            KeyStore trusted = KeyStore.getInstance("BKS");
            InputStream in = Beadict.getAppContext().getResources().openRawResource(R.raw.mytruststore);
            trusted.load(in, "pwd".toCharArray());
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trusted);
            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
            return sslContext.getSocketFactory();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public CertificatePinner getPinnedCerts() {
        return new CertificatePinner.Builder()
                .add("domain.com", "sha1/theSha=")
                .build();
    } …
    Run Code Online (Sandbox Code Playgroud)

java ssl android okhttp

18
推荐指数
5
解决办法
3万
查看次数

Node.js字符串的Hmac SHA256 base64

我正在使用java和带有节点的服务器制作应用程序,并且作为身份验证方法,我想比较两个字符串.

在java中我正在这样做:

try {
    String secret = "secret";
    String message = "Message";

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);

    String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
    System.out.println(hash);
} catch (Exception e){
    System.out.println("Error");
}
Run Code Online (Sandbox Code Playgroud)

但我仍然是node.js的新手,我正在试图弄清楚如何在那里做同样的事情.这就是我所拥有的:

var crypto = require('crypto');
var sha256 = crypto.createHash('HMAC-SHA256').update('Message').digest("base64");
Run Code Online (Sandbox Code Playgroud)

我怎样才能让他们做同样的事情呢?我仍然缺少node.js中的salt.建议?

编辑: 下面的答案帮助我找到了解决方案.如果其他Android用户有这个问题,那么这段代码对我有用:

try {
    String secret = "secret";
    String message = "Message";

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    byte[] s53 = sha256_HMAC.doFinal(message.getBytes());
    String hash = Base64.encodeToString(s53, Base64.DEFAULT);
    Log.e("beadict", hash);
} catch …
Run Code Online (Sandbox Code Playgroud)

java android node.js

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

无法通过Android Studio启动应用程序(logcat [DEAD])

在过去的两天里,我正在使用的应用程序在logcat中显示为[DEAD],当我尝试通过Android Studio中的运行按钮启动应用程序时似乎都很好,除了应用程序永远不会启动并且不会留下任何错误消息在Android Studio中,[DEAD]除外.知道这是什么吗?

在此输入图像描述

更新: 我已经能够通过以下方式从logcat中删除[DEAD]的东西:

  • 从设备中删除应用程序
  • 重新启动设备(不应该是因为它刚刚启动,因为它发生了,但以防万一)
  • 在Android Studio中使缓存无效并重新启动
  • 从Android Studio中的工具栏重新运行应用程序

这可能有助于让你重去,但我的问题仍然存在.

这是什么?

为什么会这样?

什么是正确的解决方法?

android logcat android-studio

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

angularjs $ scope.$ apply()给出了这个错误:错误:[$ rootScope:inprog]

我正在尝试更新作为$ scope一部分的页面上的一些文本.但我一直收到这个错误:

Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1]
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:6:450
at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:101:443)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:301)
at h.$scope.changeLang (http://treenovum.es/xlsmedical/js/medical-app.js:80:16)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:169:382
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:390
at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:40)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:318)
at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:372) 
Run Code Online (Sandbox Code Playgroud)

显然我做错了什么.:)关于如何解决这个问题的任何想法?我希望页面更新到范围中的新变量.

这是我用于更新的代码:

medicalApp.controller('MainCtrl', function($scope, $cookies, getTranslation) {
    getTranslation.get(function(data){
        $scope.translation = data;
    });

    $scope.changeLang = function (lang) {
        console.log(lang);
        $cookies.lang = lang;
        $scope.$apply(function(){
            getTranslation.get(function(data){
                $scope.translation = data;
                console.log(JSON.stringify($scope.translation));
            });
        });
    };
});
Run Code Online (Sandbox Code Playgroud)

html:

<body ng-controller="MainCtrl">
    ...
            <div class="header-lang col-xs-4">
                <p>
                    <a href="#" ng-click="changeLang('de')">DE</a> | 
                    <a href="#" ng-click="changeLang('fr')">FR</a></p>
            <div>{{ translation.text …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

12
推荐指数
2
解决办法
3万
查看次数