gpt4 book ai didi

android - 试图让一个简单的锁屏应用程序工作,与 Service & Reciever 作斗争

转载 作者:行者123 更新时间:2023-11-29 01:41:32 24 4
gpt4 key购买 nike

我正在努力制作我的基本锁屏应用程序。开始时,该应用程序显示一个基本 Activity 屏幕,带有一个 ImageView (显示存储中的图像)和一个 TextView(显示日期和时间)。向上滑动时, Activity 回去。我正在努力解决的问题是,当屏幕关闭和打开时,事件接收器应该发送此事件并且我的 Activity 应该再次出现在前台。事实并非如此。 我不完全确定如何使用我的服务和 broadcastreceiver 来实现这一目标。如有任何帮助,我们将不胜感激。

我的 AndroidManifest 文件:

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

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

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<receiver
android:name="com.example.kjtest2.EventsReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<service
android:enabled="true"
android:name="com.example.kjtest2.myService"/>

<activity
android:name="com.example.kjtest2.MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

我的接收者:

       import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class EventsReciever extends BroadcastReceiver {

public boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent recievedIntent) {

Log.i("Check","[BroadCastReciever] onRecieve()");

if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.i("Check","[BroadCastReciever] Screen went OFF");
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.i("Check","[BroadCastReciever] Screen went ON");

Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
else if(recievedIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
}

}

我在 Activity 中的 onCreate 代码:

    protected void onCreate(Bundle savedInstanceState) {
Log.i("event", "activity onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);

Intent intent = new Intent(this, EventsReciever.class);
setContentView(R.layout.activity_main);

/** REMOVING KEYGUARD RECEIVER **/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
setTime();
changeImage();
ImageView img2 = (ImageView)findViewById(R.id.imageView);
img2.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "Swipe left", Toast.LENGTH_SHORT)
.show();
changeImage();
Log.i("event","onSwipeLeft");
}
@Override
public void onSwipeRight() {
TextView tvDisplayDate = (TextView)findViewById(R.id.date1);
CustomDigitalClock cdc = (CustomDigitalClock)findViewById(R.id.dc1);
if (tvDisplayDate.getVisibility()==View.VISIBLE) {
tvDisplayDate.setVisibility(View.GONE);
cdc.setVisibility(View.GONE);
} else {
tvDisplayDate.setVisibility(View.VISIBLE);
cdc.setVisibility(View.VISIBLE);
}
}
@Override
public void onSwipeUp() {
Toast.makeText(MainActivity.this, "Swipe up", Toast.LENGTH_SHORT)
.show();
MainActivity.this.moveTaskToBack(true);
}
@Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Swipe down", Toast.LENGTH_SHORT)
.show();
}
});
}

最后是我的服务:

public class myService extends Service{
private NotificationManager mNM;
private int NOTIFICATION = R.string.local_service_started;
private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;

@Override
public void onCreate() {

/** INITIALIZE RECEIVER **/
//RegisterReciever();
Log.i("event", "ServiceStarted");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

// Display a notification about us starting. We put an icon in the status bar.
showNotification();
}
}

截至目前,接收器和服务尚未启动。我已经能够通过动态注册事件来启动它们,但我认为这不是必需的。在 list 中注册事件对于我的应用程序应该足够了,不是吗?

感谢您的帮助。

最佳答案

通过在 Activity onCreate() 中包含 startService(new Intent(this,MyService.class)); 语句确保您的服务正在运行

实际上需要以编程方式注册接收器, list 声明不适用于 Intent.ACTION_SCREEN_OFFIntent.ACTION_SCREEN_ON,在服务 onCreate 中注册接收器实例(),在onDestroy()

注销
 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new EventsReciever();
registerReceiver(receiver, filter);

关于android - 试图让一个简单的锁屏应用程序工作,与 Service & Reciever 作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com