小编Ath*_*ira的帖子

简单的线程管理 - Java - Android

我有一个应用程序,当用户要求过滤图像时会生成一个新线程.

这是我所拥有的唯一类型的任务,并且所有任务都同等重要.

如果我要求太多并发线程(我想要的最大值是9),线程管理器会抛出RejectedExecutionException.

在那一刻,我所做的是;

// Manage Concurrent Tasks
private Queue<AsyncTask<Bitmap,Integer,Integer>> tasks = new LinkedList<AsyncTask<Bitmap,Integer,Integer>>();

@Override
public int remainingSize() {
    return tasks.size();
}

@Override
public void addTask(AsyncTask<Bitmap, Integer, Integer> task) {
    try{
        task.execute(currentThumbnail);
        while(!tasks.isEmpty()){
            task = tasks.remove();
            task.execute(currentThumbnail);
        }
    } catch (RejectedExecutionException r){
        Log.i(TAG,"Caught RejectedExecutionException Exception - Adding task to Queue");
        tasks.add(task);
    }
}
Run Code Online (Sandbox Code Playgroud)

只需将被拒绝的任务添加到队列中,下次启动线程时,将检查队列以查看是否存在待办事项.

显而易见的问题是,如果最终任务在第一次尝试时被拒绝,它将永远不会重新启动(直到它不再需要它之后).

只是想知道我是否应该使用一个简单的模型来管理这类事情.我需要任务在完成后通知队列,所以我知道有空间,但我不确定如何.

亲切的问候,

加文

java multithreading android

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

Android:无法更改AlertDialog中显示的文本

今天我遇到了一个非常奇怪的问题.在我的游戏中,我创建了AlertDialog,以便在一个成功时向用户显示下一级别的挑战.所以,相应的代码是这样的.当游戏成功时,调用showDialog(R.id.display_success)并执行以下代码.

所以,我希望在每个调用中执行此代码.然而; 游戏只执行一次,并在每个其他执行中显示相同的AlertDialog.我的意思是,就像在创建第一个实例并且始终使用第一个实例之后不创建实例一样.我希望我能够描述我的问题.

case R.id.display_success:           
       updateGameSettings();
       message = formatLevel()
       + formatMission();
       return new AlertDialog.Builder(this)
       .setIcon(R.drawable.smiley_happy)
       .setTitle(R.string.dialog_success)
       .setMessage(message)
       .setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog,     int whichButton) {
                       startANewGame();
               }
       })
       .setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
                     finish();
               }
       })
       .create();
Run Code Online (Sandbox Code Playgroud)

android text dialog

7
推荐指数
1
解决办法
5316
查看次数

谁杀了我的服务?

我已经阅读了关于这个主题的每个Stackoverflow答案,但它们都没有奏效.

目标:始终保持我的服务全天候运行

问题:每当我的设备处于睡眠模式一小时或更长时间时,服务就会被终止


我试图解决它:

  • 返回START_STICKYonStartCommand()使用startForeground()

        public int onStartCommand(Intent intent, int flags, int startId) {
    
            notification = makeStickyNotification(); //I've simplified the irrelevant code, obviously this would be a real notification I build
    
            startForeground(1234, notification);
            return START_STICKY;
            }
    
    Run Code Online (Sandbox Code Playgroud)

这样工作正常,它甚至可以在设备内存不足时重新启动我的服务,但这还不足以解决我的设备暂停一段时间后出现的问题.

  • onCreate()我的Activity和onStartCommand()我的服务中使用Alarm Manager 来调用调用我的服务的广播接收器

            Intent ll24 = new Intent(this, AlarmReceiver.class);
    
            PendingIntent recurringLl24 = PendingIntent.getBroadcast(this, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
    
            AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
            alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*60, recurringLl24); // Every minute
    
    Run Code Online (Sandbox Code Playgroud)

这有助于保持我的服务活跃,但同样,不能解决我的问题

testing algorithm service android adb

7
推荐指数
2
解决办法
1428
查看次数

提高Android的GPS准确度

嗨我在使用gps的应用程序上工作我的测试设备是Nexus One和Htc Mytouch 3g Slide

那么有谁知道如何提高gps准确度?

gps android

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

使用ContextMenuInfo的空值调用onCreateContextMenu

我正在尝试使用ContextMenu。我已经使用SimpleCursorAdapter为一个简单的ListActivity成功完成了此操作。

继续,我想用CursorAdapter替换SimpleCursorAdapter,但仍然保留ContextMenu的行为,因此我添加了两个强制性重写功能bindView和newView

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = mLayoutInflater.inflate(R.layout.check_row, parent, false);
    registerForContextMenu(view);
    return view;
}
Run Code Online (Sandbox Code Playgroud)

请注意registerForContextMenu,它取代了ListActivity的onCreate方法中的registerForContextMenu(getListView())。我发现有必要调用onCreateContextMenu(...)

除提供给onCreateContextMenu(...)的ContextMenuInfo参数外,所有这些工作(使用预期的小部件创建的行,其上的回调正常工作等)现在为空-阻止我访问rowId。

还有另一个技巧可以执行-也许在CursorAdapter的bindView(...)方法中?

android listview cursor

5
推荐指数
1
解决办法
3469
查看次数

如何在应用程序快捷方式图标中删除徽章?

在此处输入图片说明如何在Android中的应用程序快捷方式图标中删除徽章?当我以编程方式创建应用程序快捷方式时,连同为快捷方式指定的图标一样,应用程序图标位于图标的右下角。我不要那个徽章

这是我使用的代码

    public static void addShortcutToHomeScreen(Context context)
{
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
    {
        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                .setIntent(new Intent(context, Splash.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(IconCompat.createWithResource(context, R.drawable.logo))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
    }
    else
    {
        // Shortcut is not supported by your launcher
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-shortcut pinned-shortcut

5
推荐指数
1
解决办法
1706
查看次数

在Android平台上用Java评估脚本(例如Python)

开发Android应用程序时,是否可以从Java评估一串python代码(或Perl)?

我正在尝试执行类似评估文本输入脚本的操作:

String script = text1.getText().toString();
String result = PythonRuntime.evaluate(script);
text2.setText(result);
Run Code Online (Sandbox Code Playgroud)

python java scripting android

3
推荐指数
1
解决办法
448
查看次数

Android的编程是否完全不同于iPhone的编程?

我已经为iPhone制作了应用程序,但是想要为Android编写代码,但想知道它是否值得我花时间,第二个值得我学习另一种语言,第三个值得努力的意思是我要去从中获利.此外,如果Android代码有任何相似之处,那么将很难带上iPhone(Xcode)文件,或者将我的iPhone代码复制并粘贴到Android代码中.

iphone android

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

Android 9配置设备和模拟器中的API调用显示连接错误

我正在使用Android 9模拟器,它具有系统的互联网连接,并且已在清单中添加了互联网许可。当我尝试调用api时,错误消息显示为:connection error

android network-connection android-9.0-pie

0
推荐指数
1
解决办法
752
查看次数