GVKun编程网logo

android-编辑计划中的待定意图

7

如果您想了解android-编辑计划中的待定意图的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于Android5中的意图动作调用、AndroidStudio–使用依赖于另一个Android

如果您想了解android-编辑计划中的待定意图的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于Android 5中的意图动作调用、Android Studio – 使用依赖于另一个Android库的Android库构建Android项目、Android – AppWidget的待定意图不会触发、Android – PendingIntent.FLAG_CANCEL_CURRENT – 它真的取消了alarmManager之前的待定意图吗?的有价值的信息。

本文目录一览:

android-编辑计划中的待定意图

android-编辑计划中的待定意图

我写了一个应用程序,用于在之前选择的预定时间打开/关闭WiFi.
它的工作方式非常简单:从时间选择器中选择时间,然后将其添加即可.它以编程方式从时间选择器获取数据并进行设置和报警.
我会为自己的活动写下一个代码,然后首先广播接收者,然后在此代码下写下我的问题.不用担心,我在代码中添加了注释,以便于阅读和理解.

Wifi.class:

public class WiFi extends AppCompatActivity {
private final int TEMP_REQUEST_CODE = 1;
private AlarmManager alarmManager;
private TimePicker timePicker;
private PendingIntent pendingIntent;
private Intent intent;
private Context context;
private Calendar calendar;
Intent alarmIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wifi_class);
    timePicker = (TimePicker) findViewById(R.id.timePicker1);
    timePicker.setIs24HourView(true);
    setTitle("Set WiFi off/on");
    //store context in global variable
    context = getApplicationContext();
    //creates an intent that will be used in pending intent
    initializeView();
    //creates pending intent
    createalarmIntent();
    //checks if alarm exists and sets matching text on the screen
    checkAlarmExists();

}

// I got an add button in my toolbar that saves the alarm, same as to delete 
an alarm
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onoptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tick:
            Alarm();
            finish();
            return true;
        case R.id.dismiss:
            cancelAlarm();
        default:
            return super.onoptionsItemSelected(item);
    }
}
//this method initialize intent and stores it into variable, 
WiFiService.class extends broadcastReceiver
//and all what it has to do is to turn wifi on/off
private void initializeView () {
    intent = new Intent(context, WifiService.class);

    //creating global pendingIntent variable
     pendingIntent = PendingIntent.getbroadcast(context,
            0,
            intent,
            0);
    //creating global alarmManager variable
     alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
//this method gets selected time from timepicker and calls another method to 
create an alarm
private void Alarm() {
     calendar = Calendar.getInstance();
    if (Build.VERSION.SDK_INT >= 23) {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getHour(),
                timePicker.getMinute(), 0
        );

    } else {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),
                0);
    }
    setAlarm();
}

/**
 * This method sets Alarm that will be executed even in Doze mode and will 
intent WifiServices class,
 * in the result it will turn wifi off or on
 *
 */
private PendingIntent createalarmIntent() {
     alarmIntent = new Intent(this, WifiService.class);
    return PendingIntent.getbroadcast(this, TEMP_REQUEST_CODE,
            alarmIntent, 0);
}
private void setAlarm() {
//setting alarm
    PendingIntent intent = createalarmIntent();
    if (Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAlarmClock(new 
AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent), 
intent);
}
    else {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), 
pendingIntent);
    }

    long AlarmMgs = calendar.getTimeInMillis() - 
Calendar.getInstance().getTimeInMillis();
    Toast.makeText(context, "Alarm will be executed in " + AlarmMgs / 1000 / 
60 + "min", Toast.LENGTH_SHORT).show();

}


/**
 * This method should cancel alarm that is being currently set. There is no 
 if/else statement 
 * because it always says that alarm exists (see next method)
 */
private void cancelAlarm() {

    alarmManager.cancel(createalarmIntent());
    Toast.makeText(context, "Alarm dismissed", Toast.LENGTH_SHORT).show();
    checkAlarmExists();
}

/**
 * This method checks wrether alarm is set or not and assigns that into 
 TextView
 * Unfortunately it always says it exists
 */
private void checkAlarmExists() {

    boolean alarmExists =
            (PendingIntent.getbroadcast(context,
                    0,
                    intent,
                    PendingIntent.FLAG_NO_CREATE)
                    != null);

    TextView alarmSetter = (TextView) findViewById(R.id.alarmSet);
    if (alarmExists) {
        alarmSetter.setText("Alarm is set");

    } else {
        alarmSetter.setText("Alarm is not set yet");
    }
}
    //Todo: (1): Create more than 1 alarm without replacing one before
    //Todo: (2): Cancel one of them, not all of them
    //Todo: (3): Edit those alarms 
}

WifiService类别:

public class WifiService extends broadcastReceiver {

public static final String TAG = "WiFi";

@Override
public void onReceive(Context context, Intent intent) {
//        if 
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
{

        WifiManager wifiManager = (WifiManager) 
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Log.v("Wifi", "checked");
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
            Toast.makeText(context, "Turning wifi off", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "Turned off");
        } else {
            wifiManager.setWifiEnabled(true);
            Toast.makeText(context, "turning wifi on", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "turned off");
        }
//        }
    Log.v(TAG, "Works");
}

}

好的,我在代码中留下了3个Todo:
1.我想创建1个或多个警报而不替换现有警报,我要做的就是设置挂起标志以在每次设置警报时更新?
2.如果我创建了3个警报,我想取消其中一个,指定一个.无论如何,我将这些警报存储在数据库中,所以我的想法是:
创建警报->将其添加到数据库->显示在列表中->
如果已删除->从列表中删除-> disactivate
3.编辑警报.我知道如何编辑数据库中的项目,但是如何编辑预定警报?我的意思是编辑其起火时间.是否全部与重新创建警报有关?

有人可以回答我的问题吗?提前致谢.

解决方法:

设置警报时,会将PendingIntent传递给AlarmManager. PendingIntent包装一个Intent.设置警报时,AlarmManager会删除它已计划的,与PendingIntent相匹配的警报.要确定PendingIntents是否匹配,将比较以下内容:

> PendingIntent.getbroadcast()调用中的requestCode
>意向行动
>目的类别
>意向中的数据
> Intent中的组件(包名称,类名称)

注意:Intent中的“附加”不进行比较

因此,如果我们想重新安排警报,您只需要创建一个PendingIntent,它的请求代码,ACTION和Component与上一个相同,然后再次设置警报即可. AlarmManager将删除上一个并设置新的.

如果要并行安排多个警报,则需要确保requestCodes,Components或ACTIONs不同,否则它们将相互覆盖.

Android 5中的意图动作调用

Android 5中的意图动作调用

我有这个代码,在Android 4.4和之前的版本中工作正常:

Intent intent = new Intent(Intent.ACTION_CALL);         
intent.setPackage("com.android.phone");
intent.setData(Uri.parse("tel:" + number));
context.startActivity(intent);

现在,在Android 5.0 Lollipop中,此代码不起作用,并显示此异常:

Fatal Exception: android.content.ActivityNotFoundException
No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxx pkg=com.android.phone }

In the documentation,此Intent似乎不被弃用:

任何的想法?提前致谢

解决方法:

好像包名已经改变了

com.android.phone 

com.android.server.telecom.

希望这可以帮助!

Android Studio – 使用依赖于另一个Android库的Android库构建Android项目

Android Studio – 使用依赖于另一个Android库的Android库构建Android项目

我需要使用Gradle构建一个依赖于Android库项目A的Android项目,该项目取决于另一个Android库项目B.

到目前为止,我有以下内容:

Android项目:

的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

dependencies {
    compile project(':LibA')
}

android {
    compileSdkVersion 7
    buildToolsversion "17.0.0"
}

manifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cyborg.template"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="7" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.cyborg.template.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android库A:

的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

dependencies {
    compile project(':LibB')
}


android {
    compileSdkVersion 7
    buildToolsversion "17.0.0"
} 

manifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lib.project.a"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="7"/>

</manifest>

Android库B:

的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 7
    buildToolsversion "17.0.0"
} 

manifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lib.project.b"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="7"/>

</manifest>

在尝试构建Android项目时,Android studio报告了以下错误:

Gradle: Execution Failed for task ':LibA:processDebugManifest'.
  > Manifest merging Failed. See console for more info.

那个控制台在哪里我想搜索有关错误的更多信息?

我发现了一些关于这个错误的问题,但它似乎与我的情况不一样.

启示?

谢谢,
亚当.

解决方法:

该库的清单文件当前必须具有< application />节点,即使它是空的.

这是我们将在某些时候删除的限制,但现在,只需添加它.

Android – AppWidget的待定意图不会触发

Android – AppWidget的待定意图不会触发

我有一个app小部件,其中有一个Button的布局.
单击该按钮时,会触发一个调用我的广播接收器的意图.

它工作得很好,但偶尔使用任务管理器中的“清除内存”按钮后,小部件卡住了 – 点击它什么都不做.但它仍然可以从我的应用程序接收更新,如果它正在运行.

我不确定是否未触发未决意图的事实是内存清除错误或我的错.
无论如何,这是代码:

注册待定意图(app小部件的onUpdate方法)

Intent intent = new Intent(context,ServiceControl.class);
    PendingIntent pendingIntent = PendingIntent.getbroadcast(context,intent,0);
    RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget);
    views.setonClickPendingIntent(R.id.appwidgetbutton,pendingIntent);

然后使用视图更新小部件.

以下是app小部件提供程序的解除:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:initialLayout="@layout/appwidget"
android:updatePeriodMillis="0">
</appwidget-provider>

我不希望系统调用窗口小部件更新,我只从我的应用程序本身更新它.

那么为什么未决意图会停止发射?

提前致谢.

解决方法

@Jong

@CommonsWare

嗨伙计们,我想通了. Ofc这是一个Android问题,接收器应该总是收到.
现在如何解决它?显然所有小部件都在工作,所以一定有一个简单的小部件.

我在某处读到了SO(试图找到那个人),提醒我们所有小部件类实际上是在扩展broadcastReceiver.

因此,您可以注册窗口小部件(在清单中)以接收威胁本身.因此整个系统在appwidgetprovider的类实例中是自包含的.

现在,为了与应用程序进行通信,您可以在onReceive中调用应用程序的任何静态类,如果应用程序处于活动状态,LocalbroadcastManager也不会让您失败.如果它没有激活,你的按钮应该是开始活动!

如果你想要代码,我可以详细说明.

Android – PendingIntent.FLAG_CANCEL_CURRENT – 它真的取消了alarmManager之前的待定意图吗?

Android – PendingIntent.FLAG_CANCEL_CURRENT – 它真的取消了alarmManager之前的待定意图吗?

我有以下代码,它只运行一个报警管理器:

public void runAlarm(){
   Intent intent = new Intent(context,MyReceiver.class);
          intent.setAction(ACTION_TIMEOUT);
          PendingIntent alarmIntent = PendingIntent.getbroadcast(context,intent,PendingIntent.FLAG_CANCEL_CURRENT);

          setTimeOutAlarm(TIMEOUT_MINUTES,alarmIntent);
          AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                      alarmMgr.setExact(AlarmManager.RTC,Calendar.getInstance().getTimeInMillis() + 3*60*1000,alarmIntent);

}

它工作正常.我的问题是关于挂起的意图标志:PendingIntent.FLAG_CANCEL_CURRENT

如果我两次运行此方法它是否真的取消了alarmManager中已有的其他待处理意图?怎么知道它不取消它?我只想确保使用这个标志不会造成任何泄漏.我的意图是我可以多次运行此代码,它将继续取消已经发送到alarmManager实例的先前pendingIntent.

但每当我运行此代码时,当我使用此adb命令检查时,警报计数会增加1:

adb shell dumpsys alarm | grep com.your.package

所以似乎alarmManager也许没有被取消.

解决方法

我相信你不需要取消旧的闹钟..但只是更新它.

我想你可以使用:PendingIntent.FLAG_UPDATE_CURRENT

请进行一些测试,让我知道它是否有效.

今天的关于android-编辑计划中的待定意图的分享已经结束,谢谢您的关注,如果想了解更多关于Android 5中的意图动作调用、Android Studio – 使用依赖于另一个Android库的Android库构建Android项目、Android – AppWidget的待定意图不会触发、Android – PendingIntent.FLAG_CANCEL_CURRENT – 它真的取消了alarmManager之前的待定意图吗?的相关知识,请在本站进行查询。

本文标签: