嗨我在将字符串从活动传递到广播接收器时遇到了一些麻烦。
public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
@Override
public void onReceive(Context context, Intent intent) {
mymedia = MediaPlayer.create(context, R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
String sample = intent.getExtras().getString("string");
String mydata = sample;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Rain Alert")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("from"))
.setContentText("Rainfall " + sample + intent.getStringExtra("string"))
.setAutoCancel(true)
.setSound(defaultSoundUri);
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
我的Broadcastreciver中的上述代码
以下是我的警报活动,因为我试图传递字符串
public class Alarm extends AppCompatActivity {
MyBroadcaseciver mAppReceiver = new MyBroadcaseciver();
String weather;
Double Lat, Lon;
String key = "45d263e3555d4f0d528ea032cdb1f10e";
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
t = (TextView) findViewById(R.id.t);
String rainurl = String.format("http://api.openweathermap.org/data/2.5/forecast/daily?lat=53.45523808&lon=-6.18634225&cnt=1&appid=45d263e3555d4f0d528ea032cdb1f10e");
new GetWeatherTask(t).execute(rainurl);
int i = 8;
Intent intent = new Intent(this, MyBroadcaseciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
}
private class GetWeatherTask extends AsyncTask<String, Void, String> {
private TextView t;
double rain = 0.0;
String units = "imperial";
String rainurl = String.format("http://api.openweathermap.org/data/2.5/forecast/daily?lat=53.45523808&lon=-6.18634225&cnt=2&appid=45d263e3555d4f0d528ea032cdb1f10e");
public GetWeatherTask(TextView t) {
this.t = t;
}
@Override
protected String doInBackground(String... strings) {
double rain = 0.0;
String weather = "UNDEFINED";
try {
URL rainurl = new URL(strings[0]);
HttpURLConnection rainurlConnection = (HttpURLConnection) rainurl.openConnection();
InputStream rainstream = new BufferedInputStream(rainurlConnection.getInputStream());
BufferedReader rainbufferedReader = new BufferedReader(new InputStreamReader(rainstream));
StringBuilder rainbuilder = new StringBuilder();
String raininputString;
while ((raininputString = rainbufferedReader.readLine()) != null) {
rainbuilder.append(raininputString);
}
JSONObject raintopLevel = new JSONObject(rainbuilder.toString());
//JSONObject main = topLevel.getJSONObject("main");
// weather = String.valueOf(main.getDouble("rain" +
// ""));
JSONArray listArray = raintopLevel.getJSONArray("list");
JSONObject firstObject = (JSONObject) listArray.get(0);
rain = firstObject.getDouble("clouds");
Log.d("Look at this here lad" + rain, "");
weather = "" + rain;
// Log.d("SS"+jArray.length(),"");
rainurlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
registerReceiver(mAppReceiver, new IntentFilter("MyReceiver"));
return weather;
}
@Override
protected void onPostExecute(String weather) {
t.setText(weather);
String string = "Passed" + weather;
Intent passintent = new Intent("MyReceiver");
passintent.putExtra("string", string);
sendBroadcast(passintent);
}
}
}
这是第一次从我的广播接收器发出通知时的预期效果,但几秒钟之后通知再次发出,值为空我已经尝试了几个小时并且没有任何帮助得到任何帮助表示赞赏谢谢
答案 0 :(得分:0)
在您的广播接收器中,将private void createDirectoryAndSaveFile(File fileToSave) {
try {
String folderName = "MyCreatedFolder";
String dire = Environment.getExternalStorageDirectory().toString();
File dir = new File(dire +"/"+ folderName);
boolean success = true;
if (!dir.exists()) {
success=dir.mkdirs();
}
if (success) {
File mypath = new File(dir + "/"+folderName+"/", fileToSave.getName());
new BufferedWriter(new FileWriter(mypath));
Toast.makeText(context, "Created", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Failed", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
System.out.println("----" + e.getLocalizedMessage());
}
}
替换为intent.getExtras
。