小编Pra*_*eth的帖子

Intent和PendingIntent之间的差异

我读了一些文章,似乎都做了同样的事情,我想知道启动服务之间的区别是什么:

Intent intent = new Intent(this, HelloService.class);
startService(intent);
Run Code Online (Sandbox Code Playgroud)

或者像那样:

Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 
Run Code Online (Sandbox Code Playgroud)

当我读完时,这两个做同样的事情,如果在服务中你返回一个参数START_STICKY;

android android-service

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

我在特定时间安排本地通知,使用开关但在设定时间不触发

我正在使用一个开关,当打开时会触发警报并启动通知.

最初它工作正常并在设定的时间触发警报.手动更改时间后,它开始工作奇怪的通知是在开关打开但不在指定的设定时间时立即触发.

这是交换机的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    aSwitch = (Switch) findViewById(R.id.switch1);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean switchState = prefs.getBoolean("locked", false);

    if(switchState) {
        aSwitch.setChecked(true);
    } else {
        aSwitch.setChecked(false);
    }

    aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                prefs.edit().putBoolean("locked", true).apply();
                NotificationEventReceiver.setupAlarm(getApplicationContext());
                aSwitch.setChecked(true);
            } else {
                prefs.edit().putBoolean("locked", false).apply();
                NotificationEventReceiver.cancelAlarm(getApplicationContext());
                aSwitch.setChecked(false);
            }
        }
    });
  }

 @Override
 protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent);
     setIntent(intent);
 }
Run Code Online (Sandbox Code Playgroud)

当开关打开时,它将使用WakefullBroadcast接收器触发通知 .

这是接收器类的代码:

public class NotificationEventReceiver extends WakefulBroadcastReceiver {

    private …
Run Code Online (Sandbox Code Playgroud)

notifications android local android-notifications

11
推荐指数
1
解决办法
996
查看次数

如何解决此警告"W/Atlas:指针0x0,而不是在getPreloadedDrawables?"

每当我运行我的应用程序时,我都会收到这个奇怪的警告!

我对解决它没有任何想法.有人可以解释一下这个!

*Here is the logcat.*

06-25 09:34:24.997    1721-1721/? I/art? Late-enabling -Xcheck:jni
06-25 09:34:25.957    1721-1748/com.aitrg.aitqc D/OpenGLRenderer? Render dirty regions requested: true
06-25 09:34:25.970    1721-1721/com.aitrg.aitqc D/? HostConnection::get() New Host Connection established 0xa4942590, tid 1721
06-25 09:34:25.975    1721-1721/com.aitrg.aitqc D/Atlas? Validating map...
06-25 09:34:25.979    1721-1721/com.aitrg.aitqc W/Atlas? Pointer 0x0, not in getPreloadedDrawables?
06-25 09:34:25.979    1721-1721/com.aitrg.aitqc W/Atlas? Pointer 0x0, not in getPreloadedDrawables?
06-25 09:34:25.979    1721-1721/com.aitrg.aitqc W/Atlas? Pointer 0x0, not in getPreloadedDrawables?
06-25 09:34:25.979    1721-1721/com.aitrg.aitqc W/Atlas? Pointer 0x0, not in getPreloadedDrawables?
06-25 09:34:25.979    1721-1721/com.aitrg.aitqc W/Atlas? Pointer …
Run Code Online (Sandbox Code Playgroud)

android android-emulator android-logcat

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

在运行时更改区域设置时刷新(重新创建)后台堆栈中的活动

我有一个活动说ActivityMain从这个活动我移动到另一个名为的活动ActivitySettings,在设置活动中我通过单击按钮更改应用程序区域设置,并使用重新创建我实现了我在当前活动中需要的更改但是当我按下我的` ActivityMain' 将恢复,但区域设置不会更新。

有人可以告诉我如何“重新创建”backstack 活动吗?什么是正确的方法。

我无法在刷新时调用重新创建,因为它将是无限循环

android recreate onresume

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

即使应用关闭,也会在特定时间每天触发本地通知

即使我的应用程序没有运行,我也必须每天在特定时间(比如早上7点)向我的应用程序显示通知.(已关闭)

由于通知的内容将是用户特定的,我无法使用(GCM)推送

我已经完成了很多教程和答案但是我无法在应用程序关闭(不运行)时显示通知.

编辑:使用以下代码我可以在我的应用程序运行时创建通知,如果我关闭我的应用程序通知正在显示

这是我的主要活动

public class MainActivity extends AppCompatActivity {
Button setAlarm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setAlarm = (Button) findViewById(R.id.set_alarm);
    setAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationEventReceiver.setupAlarm(getApplicationContext());
        }
    });
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}
}
Run Code Online (Sandbox Code Playgroud)

在这里,当我点击设置警报按钮时,我NotificationEventReciver会打电话

public class NotificationEventReceiver extends WakefulBroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";


public static void setupAlarm(Context context) …
Run Code Online (Sandbox Code Playgroud)

android alarmmanager android-notifications

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

如何从YouTube URL获取视频缩略图并将其设置为android中的图像视图

我想创建一个我想播放YouTube视频的应用程序.我想从我在应用程序中播放的YouTube视频网址中获取缩略图并将其设置为图像视图.任何身体都可以帮助我.

youtube video android thumbnails imageview

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

OnItemClickListener()不使用List Adapter

我通过调用名为的函数将数据提取到列表适配器getAllDishes().现在我想OnItemClickListener()在点击特定项目时添加列表,它会打开另一个活动并传递所选项目的ID.我是android的新手.欢迎所有建议.

主要活动

public class MainActivity extends ListActivity   {

 private DishOperation dishDBoperation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Button   btListe;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dishDBoperation = new DishOperation(this);
        dishDBoperation.open();

        List values = dishDBoperation.getAllDishes();


        final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

//This is what i tried
 OnItemClickListener listener = new OnItemClickListener() {
  @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Result.class);

                startActivity(intent);
                finish();
            }
         }
Run Code Online (Sandbox Code Playgroud)

java android onitemclicklistener

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

如何隐藏Toast消息的显示,长按ToolBar项目(图标)android?

有没有办法在长按ToolBar项目后隐藏吐司?

长按的android ToolBar图标或图像按钮显示祝酒词.

android android-menu android-toolbar

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

如何在活动的特定片段中禁用抽屉选项

我有一个包含5个片段的活动,我在抽屉活动中使用抽屉布局,但我想在片段2中使用(启用)抽屉,我想在剩余的片段中禁用抽屉选项.

任何人都可以帮我怎么做?

android navigation-drawer drawerlayout

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

Collections.sort(),给出"未经检查或不安全的操作"错误?

我从Java Version 8中得到错误:"使用未经检查或不安全的操作".

似乎问题来自于Collections.sort(),但问题是什么?我检查了Java Doc,一切似乎都很好,除了参数是a List,但ArrayListList我而言是一个?

import java.util.ArrayList;
import java.util.Collections;

public class Driver
{
    public static void test() 
    {
        ArrayList<Person> persons = new ArrayList<Person>();
        persons.add(new Person("Hans", "Car License"));
        persons.add(new Person("Adam", "Motorcycle License"));
        persons.add(new Person("Tom", "Car License"));
        persons.add(new Person("Kasper", "Car License"));

        System.out.println(persons);        
        Collections.sort(persons);   
        System.out.println(persons);

        System.out.println(Collections.max(persons));
        System.out.println(Collections.min(persons));
    }
}
Run Code Online (Sandbox Code Playgroud)

java collections

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