다른 것들 중에서도 알람을 설정할 수있는 Phonegap 플러그인을 만드는 중입니다.Phonegap 플러그인의 응답이없는 Android BroadcastReceiver
- 설정 보류 의도는, PI는, OS가 애플 리케이션
- 을 "다시 전화를 걸"하는 데 사용할 수있는 plugin.xml 파일 에서 그 의도를 선언 다음과 같이이 작업을 수행해야하는 방법의 나의 이해는
- 알람 요청
- OS가 PI를 사용하여 앱을 다시 전화 할 때까지 기다립니다. 파일 -
<config-file target="res/xml/config.xml" parent="/*"> <feature name="Pall"> <param name="android-package" value="com.example.Plugin"/> </feature> </config-file> <config-file target="AndroidManifest.xml" parent="/*"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <receiver android:name="com.example.plugin.ServiceAlarm" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.example.plugin.ServiceAlarm" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </config-file>
가 방송 수신기 클래스을 확장
plugin.xml에 파일 : 여기이를 위해
내가 작성한 코드입니다 ServiceAlarm.java
import org.apache.cordova.CordovaWebView;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.widget.Toast;
public class ServiceAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
//in a real world app one would do a whole lot more here
}
}
플러그인을 정의 - 파일 plugin.java
package com.example.plugin;
//various imports
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import ...
public class Plugin extends CordovaPlugin
{
private Context context;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView)
{
super.initialize(cordova, webView);
this.context = cordova.getActivity().getApplicationContext();
}
public void makeToast(String msg)
{
Toast.makeText(this.context, msg, Toast.LENGTH_LONG).show();
}
private boolean setAlarm(final CallbackContext cbc,Context ctxt,JSONArray data)
{
try
{
makeToast("Setting alarm");
Intent intent = new Intent(ctxt,ServiceAlarm.class);
intent.setAction("com.example.plugin.ServiceAlarm");
PendingIntent pendingIntent =
PendingIntent.getBroadcast(ctxt,1,intent,PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager =
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 10000,pendingIntent);
makeToast("Alarm Set");
cbc.success("Set!");
return true;
} catch(Exception e)
{
cbc.error("Failed!");
return false;
}
}
캐치 방법은 JS의 다리를 건너 오는 호출 :
@Override
public boolean execute(String action, JSONArray data, CallbackContext cbc)
throws JSONException
{
Context ctxt = this.context;
try
{
switch(action)
{
case "setalarm":return setAlarm(cbc,ctxt,data);
default:cbc.error(action + " is not known");return false;
}
}
catch (Exception e)
{
cbc.error(e.getMessage() + "xx");
return false;
}
}
}
는 코르도바 JS 브리지를 정의합니다. 파일 플러그인.JS :
phonegap plugin add /path/to/plugin
:
phonegap create /path/to/project --template jquery-mobile-starter
가 그럼 난 플러그인을 추가 :이 모든 장소와
var exec = require('cordova/exec');
var plugin =
{
setalarm:function (scbk, ecbk) {exec(scbk, ecbk, "Plugin", "setalarm",
[]);}
};
module.exports = plugin;
, 나는 JQuery와 모바일 스타트 템플릿을 사용하여 폰갭 CLI에서 폰갭 프로젝트를 생성
그리고 그 후 나는 app.js
에 init
방법을 수정 :
는 지금까지, 나는 다음을 모두 수행 한 :이 된 후에 알람 10 초 트리거하기 위해이 플러그인에서 방법을 노출 플러그인
- 을
- 는
- 이 플러그인을 사용하는 응용 프로그램을 만든 응용 프로그램 매니페스트 파일
- 이러한 이벤트를 처리 할 수있는 의도를 선언 OS에 의해 아래로 보내 알람 이벤트를 처리하는 방송 수신기를 만든 세트 0 응용 프로그램에 대한
- 는
init
방법에서
응용이 제대로 준수 알람을 설정하고 내가 코딩 한 모든 다양한 진단 토스트를 불을 시동 할 때. 그러나, 결코 응답을 실제 경보에 행사.
분명히 여기 뭔가 잘못되었거나 시스템이 경보를 처리하는 방법을 잘못 이해하고 있습니다. 나를 올바른 길로 인도 할 수있는 사람에게는 내가 가장 의무감이 많습니다. +++++++++++++
이 질문을 게시 때문에 나는 동적으로 방송 수신기를 등록하면 예상대로 사물이 작동하는 것을 발견했다.
final String SOME_ACTION = "com.example.plugin.ServiceAlarm";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
ServiceAlarm mReceiver = new ServiceAlarm(this.context);
ctxt.registerReceiver(mReceiver, intentFilter);
Intent intent = new Intent(SOME_ACTION);
여기에서 분명한 차이가 대신 텐트 생성자에 ServiceAlarm에게 클래스을 전달하는 나는 ServiceAlarm 개체 자체의 인스턴스를 전달하고 있다는 점이다.
이 작품은 매우 위안이지만 질문을 남깁니다. 저는 다른 방식, 즉 제 원래의 방식으로 여러 가지 예를 들었습니다. 그래서 그들은 잘못되었거나 내 코드 어딘가에 오류가 있습니다. config.xml 파일의 Receiver 선언.
여기 누군가가 문제를 발견 할 수 있기를 바랍니다.