简体   繁体   English

新手项目 - 从 BroadcastReceive 开始一个活动

[英]Newbie project - start an activity from BroadcastReceive

I'm trying to build a simple daily reminder app as my first Android project and I'm just stucked now.我正在尝试构建一个简单的每日提醒应用程序作为我的第一个 Android 项目,但我现在陷入困境。 Too much information from hours watching tutorials.看教程的时间太多了。 :) :)

So I would like some help to understand the code behind my idea so hopefully someone explains this with ease :)所以我想要一些帮助来理解我的想法背后的代码,所以希望有人能轻松解释这一点:)

  • Three times a day I want an alarm/reminder/splash to set off for ten seconds with different pictures and sounds depending on the alarmtime.一天三次,我希望闹钟/提醒/飞溅会根据闹钟时间以不同的图片和声音响起 10 秒钟。 An ability to tap/swipe to stop it before 10 seconds.能够在 10 秒之前点击/滑动以停止它。

alarm1 ---alarm1time08:10 ---alarm1pic1.jpg ---alarm1Sound1.mp3 alarm1 ---alarm1time08:10 ---alarm1pic1.jpg ---alarm1Sound1.mp3

alarm2 ---alarm2time12:30 ---alarm2pic1.jpg ---alarm2Sound1.mp3 alarm2 ---alarm2time12:30 ---alarm2pic1.jpg ---alarm2Sound1.mp3

alarm3 ---alarm3time18:45 ---alarm3pic1.jpg ---alarm3Sound1.mp3 alarm3 ---alarm3time18:45 ---alarm3pic1.jpg ---alarm3Sound1.mp3

So how do I do this?那么我该怎么做呢?

I'm using Android Studio 2.3 (to slow computer for 3.x...), Java and my phones are Lollipop 5.1 API level 22 and Marshmallow 6.0 API level 23我正在使用 Android Studio 2.3(为 3.x 降低计算机速度...),Java 和我的手机是 Lollipop 5.1 API 级别 22 和 Marshmallow 6.0 API 级别 23

This is my code that actually works without error..这是我的代码,实际上没有错误。

But now i don't know how open an activity with my image and sounds.但现在我不知道如何用我的图像和声音打开一个活动。 I got a RED startActivity when I tried it in hRec.当我在 hRec 中尝试时,我得到了一个 RED startActivity。

MainActivity.java主活动.java

 package com.hlm.myreminder; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import java.util.Calendar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int hHour1 = 08; int hMin1 = 10; Calendar hCal1 = Calendar.getInstance(); hCal1.set(Calendar.HOUR_OF_DAY, hHour1); hCal1.set(Calendar.MINUTE, hMin1); hCal1.set(Calendar.SECOND, 0); Log.v("hLogging","cal set"); Intent hIntent = new Intent(getApplication(),hRec.class); PendingIntent hPi = PendingIntent.getBroadcast(getApplicationContext(),0,hIntent,PendingIntent.FLAG_UPDATE_CURRENT); Log.v("hLogging","intent set"); AlarmManager hAlMgr = (AlarmManager)getSystemService(ALARM_SERVICE); hAlMgr.setRepeating(AlarmManager.RTC_WAKEUP,hCal1.getTimeInMillis(),hAlMgr.INTERVAL_DAY,hPi); Log.v("hLogging","am set"); // startActivity(new Intent(this, hshowreminderpic1.class)); //works // Log.v("hLogging","goto hshowreminderpic1 done"); //works } }

hRec.java hRec.java

 package com.hlm.myreminder; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class hRec extends BroadcastReceiver { @Override public void onReceive(Context context, Intent hIntent) { Log.d("hLogging", "hRec:BroadcastReceiverAlarm"); // show hshowreminderpic1 /* startActivity gets RED */ // startActivity(new Intent(this, hshowreminderpic1.class)); // Log.v("hLogging","goto hshowreminderpic1 request"); }

AndroidManifest.xml AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hlm.myreminder"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> <application android:allowBackup="true" android:icon="@drawable/h_icon72" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".hshowreminderpic1"/> <receiver android:name=".hRec"></receiver> </application> </manifest>

You need to pass context to start your activity from hRec.java as hRec is not an activity.您需要传递上下文以从 hRec.java 开始您的活动,因为 hRec 不是活动。 So simply write所以简单地写

Intent i = new Intent(context, hshowreminderpic1.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(i);

尝试使用这个:

context.startActivity(new Intent(this, hshowreminderpic1.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM