小编Jas*_*son的帖子

如何在没有声音的情况下显示通知

如何在构建时发出不发出声音的通知?我正在建立一个通知,我的用户不喜欢它发出声音的事实.

如何将其更改为静音/无声?

我如何显示通知:

android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(main);
builder.setStyle(new android.support.v7.app.NotificationCompat.BigTextStyle().bigText(text));
builder.setSmallIcon(R.drawable.app);
builder.setContentTitle("Rooster Maandag:");
builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager = (NotificationManager) main.getSystemService(main.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Run Code Online (Sandbox Code Playgroud)

我试图在谷歌搜索,但我得到的唯一结果是如何播放声音,而不是如何播放声音...

编辑 它可能在某些人的眼中是重复的,但在我的中我找不到指定默认值的替代方法,而这个新方法称为setDefaults

java notifications android ringtone android-notifications

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

Java和Javascript之间的加密和解密将无法正常工作

编辑1

在decryptFile方法中,解密部分不会输出任何内容.

let decrypted = CryptoJS.AES.decrypt(e.target.result, CryptoJS.enc.Utf8.parse(key), {
    iv: CryptoJS.enc.Utf8.parse(iv),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});
Run Code Online (Sandbox Code Playgroud)


编辑2评论部分中给出 的链接部分解决了问题.它确实对跨平台进行加密和解密,但由于PBKDF2具有SHA256散列,它相当慢.我找不到只使用AES部分而不是PKBDF2部分的方法.


原文

我对Java和Javascript版本使用相同的密钥和IV.我无法解密用Java加密的Javascript中的文件,也无法解密用Javascript加密的Java文件.我需要这两个兼容彼此,但我无法弄清楚我是如何尝试解密以前用Java加密的Javascript中的文件.我已经成功地在两者之间实现了解密和加密文本,但是当我想要解密一个用Java加密的文件时,它就无法工作了.

用Java加密/解密文件:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test {
    private SecretKey secretKey;
    private IvParameterSpec ivParameterSpec;

    private String key = "ThisIsMyGreatKey";
    private byte[] ivKey = "ABCDEFGHabcdefgh".getBytes();

    public static void main(String[] args) {
        try {
            new Test().run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void run() {
        ivParameterSpec …
Run Code Online (Sandbox Code Playgroud)

javascript java encryption cryptography aes

16
推荐指数
2
解决办法
1472
查看次数

MainActivity.this不是一个封闭的类AsyncTask

我正在尝试第一次创建一个AsyncTask,但我没有太多运气.

我的AsyncTask需要从服务器获取一些信息,然后将新布局添加到主布局以显示此信息.

一切似乎都或多或少清晰,但错误信息"MainActivity不是一个封闭的类"困扰着我.

没有其他人似乎有这个问题,所以我想我错过了一些非常明显的东西,我只是不知道它是什么.

另外,我不确定我是否使用正确的方法来获取上下文,并且因为我的应用程序没有编译所以我无法测试它.

非常感谢您的帮助.

这是我的代码:

public class BackgroundWorker extends AsyncTask<Context, String, ArrayList<Card>> {
    Context ApplicationContext;

    @Override
    protected ArrayList<Card> doInBackground(Context... contexts) {
        this.ApplicationContext = contexts[0];//Is it this right way to get the context?
        SomeClass someClass = new SomeClass();

        return someClass.getCards();
    }

    /**
     * Updates the GUI before the operation started
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    /**
     * Updates the GUI after operation has been completed
     */
    protected void onPostExecute(ArrayList<Card> cards) {
        super.onPostExecute(cards);

        int counter = …
Run Code Online (Sandbox Code Playgroud)

java android android-asynctask

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

如何在Android上获取本地网络中所有设备的IP地址和名称

我想用java看到我网络上的所有连接设备,但是我无法让它工作.我在下面附上了一些截图,说明我希望如何输出.我想要名称(例如"TP Link Router"或"Nexus 5X")和IP地址.

我在谷歌和stackoverflow上搜索了很多,但似乎没有什么对我有用.即使是GitHub也没有有效的代码.我尝试搜索UPnP,局域网,子网等,但一无所获.

InetAddress localhost = InetAddress.getLocalHost();
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++) {
    ip[3] = (byte)i;
    InetAddress address = InetAddress.getByAddress(ip);
    if (address.isReachable(1000)) {
        System.out.println(address + address.getHostAddress() + address.getAddress() + address.getHostName() + address.getCanonicalHostName());
    }
}
Run Code Online (Sandbox Code Playgroud)

例1 例2

事实上,我确实找到了一个重复(某种)问题,但一年多来都没有回答.资源

java networking android lan

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

不允许后台执行.Android O pendingintent

我有一个服务,安排pendingintent开始我的通知.但是,因为Android OI出现此错误.我做了一些研究,偶然发现context.registerReceiver,但这似乎并没有解决问题.

错误:

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:my.great.package flg=0x4000010 (has extras) } to com.google.android.googlequicksearchbox/com.google.android.apps.gsa.googlequicksearchbox.GelStubAppWatcher
Run Code Online (Sandbox Code Playgroud)

```

我的待遇:

Intent myNotification = new Intent("services.notifications.Notification");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) (Math.random() * Integer.MAX_VALUE), myNotification, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
alarmManager.setExact(AlarmManager.RTC_WAKEUP, day.getTimeInMillis(), pendingIntent);
Run Code Online (Sandbox Code Playgroud)

我的通知:

public class Notification extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.registerReceiver(this, new IntentFilter());

        try {
            WakeLock wakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(1, "NotificationWakeLock");
            wakeLock.acquire(10000);

            try {
                scheduleNotification(context, intent);
            } finally { …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-intent android-notifications android-8.0-oreo

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

让应用程序在后台运行

我想保持我的应用程序在后台运行
我有一个应用程序将用户的位置发送到我们的服务器
我有以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    updateWithNewLocation(null);

    locationManager.requestLocationUpdates(provider, (10*60*1000), 10,
                                           locationListener);
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  public void updateWithNewLocation(Location location) …
Run Code Online (Sandbox Code Playgroud)

android background

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

如何在Android应用程序中设置日夜主题

我创建了一个Android应用程序,它使用首选项列表来设置日夜模式主题.

我的styles.xml

<style name="Theme.FullScreen" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen"></style>

<style name="PreferencesTheme" parent="android:Theme.Light">
    <item name="android:background">#FFEAEAEA</item>
</style>

<style name="PreferencesTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@drawable/ic_icon_settings</item>
</style>
Run Code Online (Sandbox Code Playgroud)

有谁知道如何将night和day模式应用到我的styles.xml

android

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

当onDatasetChanged方法包含异步网络调用Android时,Widget ListView不会刷新

我想将互联网上的汽车列表加载到Widget中的ListView中.当我在onDatasetChanged方法中请求汽车时,它不会更新列表.然而,当我手动点击我的刷新按钮时,它会刷新它.可能是什么问题呢?为了提供尽可能多的信息,我将完整的源代码放入ZIP文件中,该文件可以解压缩并在Android Studio中打开.我也将它嵌入下面.注意:我使用RxJava作为异步请求,代码是用Kotlin编写的,尽管这个问题不是每个语言都绑定的.

CarsWidgetProvider

class CarsWidgetProvider : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        Log.d("CAR_LOG", "Updating")
        val widgets = appWidgetIds.size

        for (i in 0 until widgets) {
            Log.d("CAR_LOG", "Updating number $i")
            val appWidgetId = appWidgetIds[i]

            // Get the layout
            val views = RemoteViews(context.packageName, R.layout.widget_layout)
            val otherIntent = Intent(context, ListViewWidgetService::class.java)
            views.setRemoteAdapter(R.id.cars_list, otherIntent)

            // Set an onclick listener for the action and the refresh button
            views.setPendingIntentTemplate(R.id.cars_list, getPendingSelfIntent(context, "action"))
            views.setOnClickPendingIntent(R.id.refresh, getPendingSelfIntent(context, "refresh"))

            // Tell the AppWidgetManager …
Run Code Online (Sandbox Code Playgroud)

android widget kotlin rx-java rx-java2

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

在后台运行持久的 Web Worker 而不会受到限制

我想在后台运行一个网络工作者(在移动设备上,它在桌面上工作),它通过发送 HTTP 请求每分钟执行一个操作。不幸的是,该网站在后台运行大约 5 分钟后,将不再发送任何 HTTP 请求...一旦您返回该网站,所有网络请求都将恢复。

似乎网络工作者不允许在后台运行,至少在移动设备上不允许,因为它在桌面上运行良好。我找不到任何消息来源说明为什么它受到限制以及可以采取什么措施。我在 2017 年发现了一个类似的问题,那是 4 年前的事了,但没有得到我想要的答案,而且可能已经过时了。

我想到了以下解决方案:

  • 通常在原生 Android 中,您可以显示一条通知,以确保您的应用程序在后台持续运行,我想知道这是否也可以与 Web Worker 一起在 Web 上运行。
  • 如果您在 Web Worker 中打开 Web 套接字,它会让您的 Web Worker 在后台保持活动状态吗?
  • 渐进式网络应用程序。不幸的是,似乎没有保持活跃状态​​。

对此我能做些什么吗?

javascript xmlhttprequest web-worker service-worker progressive-web-apps

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

Robocode如何获取敌人的坐标

我一直在尝试获取敌人的坐标,以便我可以对他们所在的位置采取行动。我使用的代码似乎不起作用:

    double absBearing = e.getBearingRadians() + e.getHeadingRadians();
    double ex = getX() + e.getDistance() * Math.sin(absBearing);
    double ey = getY() + e.getDistance() * Math.cos(absBearing);
Run Code Online (Sandbox Code Playgroud)

我似乎正在得到奇怪的回报,这些回报给我的价值大于领域的大小,甚至是负数字,有人对如何修改这段代码以获取敌人的代码X以及Y与我X和相同的方式有任何想法Y吗?

java robocode

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