关于ddpush推动实现抖动视频的使用

时间:2024-08-10 20:34:08

/**

  //开机之后打开服务  开机成功打开服务ddpushService
*
*/

<!-- 开机广播 -->
<receiver android:name="com.receiver.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />

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

//打开开机启动服务

<service android:name="com.service.BootCompletedService" />
<service android:name="com.service.UpdateService" />

<!-- 常驻服务,避免杀死app -->
<service android:name="com.service.DDPushService" />

//为了确保服务不会被断网影响

/**
* 网络监听
* @author admin
*
*/
public class NetworkReceiver extends BroadcastReceiver {

private static final String TAG = NetworkReceiver.class.getSimpleName();

public NetworkReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {

Tools.Log("网络变化");
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean hasNetWork = Tools.hasNetwork(context);
if (hasNetWork) {
Tools.startService(context,"RESET");
BaseApplication.initGTService();
// Tools.getAddress();
// 网络变化且有网
if (Tools.hasWifi(context)){
Tools.ShowToastTemporary(context,"网络变化>有网wifi");
}else {
Tools.ShowToastTemporary(context, "网络变化>有网4G");
}
// 如果以没登录,就执行登录
if (BaseApplication.user==null){
BaseApplication.login();
}
}else { // 网络断开连接
Tools.ShowToastTemporary(context, "网络变化>没有网");
BaseApplication.upDataPushIdSucceed = false;
}
}
else {
//只要打开有网络 就运行ddpushservice
boolean hasNetWork = Tools.hasNetwork(context);
if (hasNetWork) {
Intent it = new Intent(context, DDPushService.class);
it.putExtra("CMD", "start_service");
context.startService(it);
it = null;
}
}
}

//抖动视频处理
//在没有打开应用用通知的形式发送通知
else if ("020".equals(code)) {
//查看车机抖动视频
videoName = JsonParser.getShakeVideoName(text);
if (!"com.icarphone".equals(Tools.getTopActivityPackageName(context))) {
LogUtils.v(TAG, "020 com.icarphone is not top Activity package");
int icon = R.drawable.ic_launcher;
String tickerText = "您有一条抖动视频!";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.mipmap.app);
contentView.setTextViewText(R.id.text, "您收到了一条抖动视频,请注意查看!");
notification.contentView = contentView;//
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra(MainActivity.P_VIDEO_NAME, videoName);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntent;

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(mId++, notification);

//消息处理
int newMsg = UtilSharedPreference.getIntValue(context, Constants.MESSAGE_NUMBER);
newMsg++;
UtilSharedPreference.saveInt(context, Constants.MESSAGE_NUMBER, newMsg);
} else {
//如果应用打开直接跳出弹框
//查看车机抖动视频
LogUtils.v(TAG, "020 com.icarphone is top Activity package");

shakeCarPush(videoName);
}
MsgBean mb = new MsgBean();
mb.curMis = System.currentTimeMillis();
mb.displayName = "您收到了一条抖动视频,请点击查看!";
mb.fileName = videoName;
mb.cmd = code;
mDbHelper.save(mb);
mb = null;
}

//弹出弹框打开视频

private void shakeCarPush(final String videoName) {
Activity act = getCurrentActivity();
LogUtils.v(TAG, "shakeCarPush activity = " + act.getLocalClassName());
final Dialog sendDia = new Dialog(act);
View v = LayoutInflater.from(act).inflate(R.layout.shake_alarm, null);
tv_data = (TextView) v.findViewById(R.id.tv_data);
btn_video = (Button) v.findViewById(R.id.btn_video);
btn_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (sendDia!= null)
sendDia.dismiss();
//获取抖动视频
postToShakeVideo(videoName);
} catch (Exception e) {
e.printStackTrace();
}

}
});
String recTime = Tools.getTimeDetailes(System.currentTimeMillis());
tv_data.setText(recTime);
sendDia.requestWindowFeature(Window.FEATURE_NO_TITLE);
sendDia.setContentView(v);
Window window = sendDia.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setGravity(Gravity.CENTER);
//WindowManager windowManager = getCurrentActivity().getWindow().getWindowManager();
//Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams params = window.getAttributes();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = getScreenWidth(this);
window.setAttributes(params);
sendDia.show();
}

//获取最后压入寨的act

private Activity getCurrentActivity() {
Activity act = BaseActivity.currentActivity();
//这里的act BaseActivity.currentActivity()==基类BaseActivity oncreat=this
String name = act.getLocalClassName();
LogUtils.v(TAG, "getCurrentActivity() act" + name);
return act;
}

//下载成功 播放视频
else if ("021".equals(code)) {
LogUtils.v(TAG, " msg code = 021");
handler.removeMessages(MSG_COUNTER);
if(null == dialog || (null!=dialog && dialog.isShowing())) {
if (dialog != null)
dialog.dismiss();

String url = JsonParser.getShakeVideoUrl(text.trim());

MsgBean msgBean = new MsgBean();
msgBean.fileUrl = url;
msgBean.fileName = Utils.getUrlFileName(url);
msgBean.curMis = System.currentTimeMillis();
msgBean.cmd = code;
mDbHelper.saveShakeMsg(msgBean);
msgBean = null;

Intent dialogIntent = new Intent(getBaseContext(), ShakeVideoActivitys.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("url", url);
getApplication().startActivity(dialogIntent);
}

//点击通知 传videoName
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra(MainActivity.P_VIDEO_NAME, videoName);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notification.contentIntent = contentIntent;

/***
* 从消息页面点击消息会进入到MainActivity这个页面,根据Intent传入的参数处理
* @param it
*/
private void dealVideo(Intent it) {
String videoName = it.getStringExtra(P_VIDEO_NAME);

if (!TextUtils.isEmpty(videoName)) {
Intent serviceIt = new Intent(this, DDPushService.class);
serviceIt.putExtra("CMD", "99");
serviceIt.putExtra("videoName", videoName);
startService(serviceIt);
serviceIt = null;
}
}
@Override
protected void onNewIntent(Intent it) {
dealVideo(it);
super.onNewIntent(it);
}

//载mainActivity中启动service 防止被杀死

// 启动ddpservice,防止被杀死
DDPushStart.start(context);

//载ddpushStart中处理启动服务
public static void start(Context context) {
Intent startSrv = new Intent(context, DDPushService.class);
startSrv.putExtra("CMD", "RESET");
context.startService(startSrv);
}

else if (cmd.equals("RESET")) {
if (wakeLock != null && wakeLock.isHeld() == false) {
wakeLock.acquire();
}
//发送推送
resetClient();

//消息界面 接收到推送进行处理
lv_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!isEdit) {
MsgBean n = news.get(position);
if (null == n)
return ;
if ("005".equals(n.cmd)) {

String FileUrl = n.fileUrl;
PhotoMsg photoMsg = new PhotoMsg();
photoMsg.setImageUrl(FileUrl);
List<PhotoMsg> list = new ArrayList<>();
list.add(photoMsg);
Bundle bundle = new Bundle();
bundle.putSerializable("list", (Serializable) list);
bundle.putInt("imgPos",0);
ScreenSwitch.startActivity(context, /*MessageGalleryActivity*/ImagePreviewActivity.class, bundle);

} else if ("021".equals(n.cmd) || "009".equals(n.cmd)) {

String FileUrl = n.fileUrl;
Bundle bundle = new Bundle();
bundle.putString("url", FileUrl);
ScreenSwitch.startActivity(context, ShakeVideoActivitys.class, bundle);
} else if ("020".equals(n.cmd)) {
Intent serviceIt = new Intent(context, DDPushService.class);
serviceIt.putExtra("CMD", "99");
serviceIt.putExtra("videoName", n.fileName);
startService(serviceIt);
serviceIt = null;
}
} else {
MsgBean n = news.get(position);
if (n.isSelected) {
n.isSelected = false;
} else {
n.isSelected = true;
}

if (isHasSel()) {
btnDelete.setBackgroundResource(R.mipmap.btn_white_nor);
} else {
btnDelete.setBackgroundResource(R.mipmap.btn_white_pre);
}
adapter.onReflush(context, news);
}

}
});

<!-- DDPUsh -->
<receiver android:name="com.service.DDPushTickAlarmReceiver" />